Skip to content

Commit 35b6016

Browse files
authored
Merge pull request github#12153 from github/mbg/fix/msbuild-on-macos-arm
C#: Improve C# autobuilder compatibility with Arm-based Macs
2 parents 7cfe15c + eab3c6d commit 35b6016

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
131131

132132
bool IBuildActions.IsWindows() => IsWindows;
133133

134+
public bool IsMacOs { get; set; }
135+
136+
bool IBuildActions.IsMacOs() => IsMacOs;
137+
138+
public bool IsArm { get; set; }
139+
140+
bool IBuildActions.IsArm() => IsArm;
141+
134142
string IBuildActions.PathCombine(params string[] parts)
135143
{
136144
return string.Join(IsWindows ? '\\' : '/', parts.Where(p => !string.IsNullOrWhiteSpace(p)));

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
145145

146146
bool IBuildActions.IsWindows() => IsWindows;
147147

148+
public bool IsMacOs { get; set; }
149+
150+
bool IBuildActions.IsMacOs() => IsMacOs;
151+
152+
public bool IsArm { get; set; }
153+
154+
bool IBuildActions.IsArm() => IsArm;
155+
148156
public string PathCombine(params string[] parts)
149157
{
150158
return string.Join(IsWindows ? '\\' : '/', parts.Where(p => !string.IsNullOrWhiteSpace(p)));

csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Http;
88
using System.Diagnostics.CodeAnalysis;
99
using System.Threading.Tasks;
10+
using System.Runtime.InteropServices;
1011

1112
namespace Semmle.Autobuild.Shared
1213
{
@@ -98,6 +99,18 @@ public interface IBuildActions
9899
/// </summary>
99100
bool IsWindows();
100101

102+
/// <summary>
103+
/// Gets a value indicating whether we are running on macOS.
104+
/// </summary>
105+
/// <returns>True if we are running on macOS.</returns>
106+
bool IsMacOs();
107+
108+
/// <summary>
109+
/// Gets a value indicating whether we are running on arm.
110+
/// </summary>
111+
/// <returns>True if we are running on arm.</returns>
112+
bool IsArm();
113+
101114
/// <summary>
102115
/// Combine path segments, Path.Combine().
103116
/// </summary>
@@ -203,6 +216,12 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,
203216

204217
bool IBuildActions.IsWindows() => Win32.IsWindows();
205218

219+
bool IBuildActions.IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
220+
221+
bool IBuildActions.IsArm() =>
222+
RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ||
223+
RuntimeInformation.ProcessArchitecture == Architecture.Arm;
224+
206225
string IBuildActions.PathCombine(params string[] parts) => Path.Combine(parts);
207226

208227
void IBuildActions.WriteAllText(string filename, string contents) => File.WriteAllText(filename, contents);

csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
using Semmle.Util.Logging;
2+
using System;
23
using System.Linq;
4+
using System.Runtime.InteropServices;
35

46
namespace Semmle.Autobuild.Shared
57
{
8+
internal static class MsBuildCommandExtensions
9+
{
10+
/// <summary>
11+
/// Appends a call to msbuild.
12+
/// </summary>
13+
/// <param name="cmdBuilder"></param>
14+
/// <param name="builder"></param>
15+
/// <returns></returns>
16+
public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder)
17+
{
18+
var isArmMac = builder.Actions.IsMacOs() && builder.Actions.IsArm();
19+
20+
// mono doesn't ship with `msbuild` on Arm-based Macs, but we can fall back to
21+
// msbuild that ships with `dotnet` which can be invoked with `dotnet msbuild`
22+
// perhaps we should do this on all platforms?
23+
return isArmMac ?
24+
cmdBuilder.RunCommand("dotnet").Argument("msbuild") :
25+
cmdBuilder.RunCommand("msbuild");
26+
}
27+
}
28+
629
/// <summary>
730
/// A build rule using msbuild.
831
/// </summary>
932
public class MsBuildRule : IBuildRule<AutobuildOptionsShared>
1033
{
11-
/// <summary>
12-
/// The name of the msbuild command.
13-
/// </summary>
14-
private const string msBuild = "msbuild";
15-
1634
public BuildScript Analyse(IAutobuilder<AutobuildOptionsShared> builder, bool auto)
1735
{
1836
if (!builder.ProjectsOrSolutionsToBuild.Any())
@@ -57,7 +75,7 @@ BuildScript GetNugetRestoreScript() =>
5775
Script;
5876
var nugetRestore = GetNugetRestoreScript();
5977
var msbuildRestoreCommand = new CommandBuilder(builder.Actions).
60-
RunCommand(msBuild).
78+
MsBuildCommand(builder).
6179
Argument("/t:restore").
6280
QuoteArgument(projectOrSolution.FullPath);
6381

@@ -95,7 +113,7 @@ BuildScript GetNugetRestoreScript() =>
95113
command.RunCommand("set Platform=&& type NUL", quoteExe: false);
96114
}
97115

98-
command.RunCommand(msBuild);
116+
command.MsBuildCommand(builder);
99117
command.QuoteArgument(projectOrSolution.FullPath);
100118

101119
var target = builder.Options.MsBuildTarget ?? "rebuild";

csharp/scripts/create-extractor-pack.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
66
dotnet_platform="linux-x64"
77
elif [[ "$OSTYPE" == "darwin"* ]]; then
88
platform="osx64"
9-
dotnet_platform="osx-x64"
9+
if [[ $(uname -m) == 'arm64' ]]; then
10+
dotnet_platform="osx-arm64"
11+
else
12+
dotnet_platform="osx-x64"
13+
fi
1014
else
1115
echo "Unknown OS"
1216
exit 1

0 commit comments

Comments
 (0)