Compare commits

...

2 Commits

Author SHA1 Message Date
Tamas Vajk
7a1483d8b5 C#: Change IsARM to Apple silicon check 2023-11-13 14:23:59 +01:00
Cornelius Riemenschneider
1223907b57 Trial removing mono from path for the msbuild test. 2023-11-13 10:50:14 +01:00
5 changed files with 47 additions and 12 deletions

View File

@@ -145,9 +145,9 @@ namespace Semmle.Autobuild.Cpp.Tests
bool IBuildActions.IsMacOs() => IsMacOs; 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) string IBuildActions.PathCombine(params string[] parts)
{ {

View File

@@ -159,9 +159,9 @@ namespace Semmle.Autobuild.CSharp.Tests
bool IBuildActions.IsMacOs() => IsMacOs; 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) public string PathCombine(params string[] parts)
{ {

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Xml; using System.Xml;
using Semmle.Util; using Semmle.Util;
@@ -119,10 +120,10 @@ namespace Semmle.Autobuild.Shared
bool IsMacOs(); bool IsMacOs();
/// <summary> /// <summary>
/// Gets a value indicating whether we are running on arm. /// Gets a value indicating whether we are running on Apple Silicon.
/// </summary> /// </summary>
/// <returns>True if we are running on arm.</returns> /// <returns>True if we are running on Apple Silicon.</returns>
bool IsArm(); bool IsRunningOnAppleSilicon();
/// <summary> /// <summary>
/// Combine path segments, Path.Combine(). /// Combine path segments, Path.Combine().
@@ -240,9 +241,25 @@ namespace Semmle.Autobuild.Shared
bool IBuildActions.IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); bool IBuildActions.IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
bool IBuildActions.IsArm() => bool IBuildActions.IsRunningOnAppleSilicon()
RuntimeInformation.ProcessArchitecture == Architecture.Arm64 || {
RuntimeInformation.ProcessArchitecture == Architecture.Arm; var thisBuildActions = (IBuildActions)this;
if (!thisBuildActions.IsMacOs())
{
return false;
}
try
{
var res = 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); string IBuildActions.PathCombine(params string[] parts) => Path.Combine(parts);

View File

@@ -15,12 +15,12 @@ namespace Semmle.Autobuild.Shared
/// <returns></returns> /// <returns></returns>
public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder) public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder)
{ {
var isArmMac = builder.Actions.IsMacOs() && builder.Actions.IsArm(); var IsRunningOnAppleSiliconMac = builder.Actions.IsMacOs() && builder.Actions.IsRunningOnAppleSilicon();
// mono doesn't ship with `msbuild` on Arm-based Macs, but we can fall back to // 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` // msbuild that ships with `dotnet` which can be invoked with `dotnet msbuild`
// perhaps we should do this on all platforms? // perhaps we should do this on all platforms?
return isArmMac ? return IsRunningOnAppleSiliconMac ?
cmdBuilder.RunCommand("dotnet").Argument("msbuild") : cmdBuilder.RunCommand("dotnet").Argument("msbuild") :
cmdBuilder.RunCommand("msbuild"); cmdBuilder.RunCommand("msbuild");
} }

View File

@@ -1,6 +1,24 @@
import os
from create_database_utils import * from create_database_utils import *
from diagnostics_test_utils import * from diagnostics_test_utils import *
def is_running_on_apple_silicon():
arch = subprocess.Popen(['sysctl', 'machdep.cpu.brand_string'], stdout=subprocess.PIPE)
output, errors = arch.communicate()
if b'apple' in output.lower():
return True
return False
# if on ARM runners, remove Mono from the path, so we're using
# dotnet restore instead of nuget.exe restore - on ARM machines
# we run dotner msbuild (instead of the mono-provided msbuild.exe)
# so we need to match the restore command, too.
platform_name = sys.platform.lower()
if platform_name.startswith("darwin") and is_running_on_apple_silicon():
os.environ["PATH"] = os.environ["PATH"].replace("/Library/Frameworks/Mono.framework/Versions/Current/Commands:", "")
# force CodeQL to use MSBuild by setting `LGTM_INDEX_MSBUILD_TARGET` # force CodeQL to use MSBuild by setting `LGTM_INDEX_MSBUILD_TARGET`
run_codeql_database_create([], db=None, lang="csharp", extra_env={ 'LGTM_INDEX_MSBUILD_TARGET': 'Build' }) run_codeql_database_create([], db=None, lang="csharp", extra_env={ 'LGTM_INDEX_MSBUILD_TARGET': 'Build' })
check_diagnostics() check_diagnostics()