From e861af5d8845425347a648895f8ceb21630ec68e Mon Sep 17 00:00:00 2001 From: Omair Majid Date: Wed, 24 Jul 2024 09:33:41 -0400 Subject: [PATCH] Make it possible to build/run tests on linux-arm64 I wanted to run the roslyn test suite on Linux on arm64. Building everything worked, but running tests would fail with errors like: Errors Microsoft.CodeAnalysis.CSharp.Emit.UnitTests_1 Could not find 'dotnet' host for the 'X64' architecture. You can resolve the problem by installing the 'X64' .NET. With the changes in this commit, I can build and run all tests using: $ ./eng/build.sh --restore --build --pack $ ./eng/build.sh --test I would, eventually, like to be able to run the tests on other platforms, including s390x and ppc64le. Hopefully that might help identify/fix issues like https://github.com/dotnet/roslyn/pull/74392 earlier. One thing I am not too happy about in this change is how many places need an explicit addition of "linux-arm64". That won't scale as we want to add more architectures. --- eng/Directory.Packages.props | 2 ++ eng/targets/ILAsm.targets | 9 +++++++- eng/targets/ILDAsm.targets | 9 +++++++- .../Test/Core/Metadata/IlasmUtilities.cs | 21 ++++++++++++++++++- src/Tools/BuildValidator/IldasmUtilities.cs | 21 ++++++++++++++++++- src/Tools/Source/RunTests/Options.cs | 16 +++++++++++++- 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/eng/Directory.Packages.props b/eng/Directory.Packages.props index b74d3474e8297..512cc36b8a7a3 100644 --- a/eng/Directory.Packages.props +++ b/eng/Directory.Packages.props @@ -290,9 +290,11 @@ + + diff --git a/eng/targets/ILAsm.targets b/eng/targets/ILAsm.targets index edc2368e0ebad..9fa6580b2ce56 100644 --- a/eng/targets/ILAsm.targets +++ b/eng/targets/ILAsm.targets @@ -12,6 +12,12 @@ false false + + PreserveNewest + runtimes + false + false + PreserveNewest runtimes @@ -26,7 +32,8 @@ + - \ No newline at end of file + diff --git a/eng/targets/ILDAsm.targets b/eng/targets/ILDAsm.targets index 9e94a720804b8..2992ece5a1d44 100644 --- a/eng/targets/ILDAsm.targets +++ b/eng/targets/ILDAsm.targets @@ -11,6 +11,12 @@ false false + + PreserveNewest + runtimes + false + false + PreserveNewest runtimes @@ -25,7 +31,8 @@ + - \ No newline at end of file + diff --git a/src/Compilers/Test/Core/Metadata/IlasmUtilities.cs b/src/Compilers/Test/Core/Metadata/IlasmUtilities.cs index c67ce4bf0e343..73766ece1da15 100644 --- a/src/Compilers/Test/Core/Metadata/IlasmUtilities.cs +++ b/src/Compilers/Test/Core/Metadata/IlasmUtilities.cs @@ -6,6 +6,7 @@ using System; using System.IO; +using System.Runtime.InteropServices; using Roslyn.Test.Utilities; using Roslyn.Utilities; using Xunit; @@ -22,6 +23,24 @@ public static DisposableFile CreateTempAssembly(string declarations, bool prepen return new DisposableFile(assemblyPath); } + private static string GetArchitecture() => RuntimeInformation.ProcessArchitecture switch + { +#if NET8_0_OR_GREATER + System.Runtime.InteropServices.Architecture.Arm => "arm", + System.Runtime.InteropServices.Architecture.Arm64 => "arm64", + System.Runtime.InteropServices.Architecture.Armv6 => "armv6", + System.Runtime.InteropServices.Architecture.LoongArch64 => "loongarch64", + System.Runtime.InteropServices.Architecture.Ppc64le => "ppc64le", + System.Runtime.InteropServices.Architecture.S390x => "s390x", + System.Runtime.InteropServices.Architecture.Wasm => "wasm", + System.Runtime.InteropServices.Architecture.X86 => "x86", + System.Runtime.InteropServices.Architecture.X64 => "x64", + _ => throw new NotImplementedException() +#else + _ => "x64" +#endif + }; + private static string GetIlasmPath() { if (ExecutionConditionUtil.IsWindowsDesktop) @@ -46,7 +65,7 @@ private static string GetIlasmPath() } else if (ExecutionConditionUtil.IsLinux) { - ridName = "linux-x64"; + ridName = $"linux-{GetArchitecture()}"; } else { diff --git a/src/Tools/BuildValidator/IldasmUtilities.cs b/src/Tools/BuildValidator/IldasmUtilities.cs index b617c37ff759b..70529bbf132fd 100644 --- a/src/Tools/BuildValidator/IldasmUtilities.cs +++ b/src/Tools/BuildValidator/IldasmUtilities.cs @@ -12,6 +12,24 @@ namespace BuildValidator { internal static class IldasmUtilities { + private static string GetArchitecture() => RuntimeInformation.ProcessArchitecture switch + { +#if NET8_0_OR_GREATER + System.Runtime.InteropServices.Architecture.Arm => "arm", + System.Runtime.InteropServices.Architecture.Arm64 => "arm64", + System.Runtime.InteropServices.Architecture.Armv6 => "armv6", + System.Runtime.InteropServices.Architecture.LoongArch64 => "loongarch64", + System.Runtime.InteropServices.Architecture.Ppc64le => "ppc64le", + System.Runtime.InteropServices.Architecture.S390x => "s390x", + System.Runtime.InteropServices.Architecture.Wasm => "wasm", + System.Runtime.InteropServices.Architecture.X86 => "x86", + System.Runtime.InteropServices.Architecture.X64 => "x64", + _ => throw new NotImplementedException() +#else + _ => "x64" +#endif + }; + private static string GetIldasmPath() { var ildasmExeName = PlatformInformation.IsWindows ? "ildasm.exe" : "ildasm"; @@ -27,7 +45,8 @@ private static string GetIldasmPath() } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - ridName = "linux-x64"; + ridName = $"linux-{GetArchitecture()}"; + } else { diff --git a/src/Tools/Source/RunTests/Options.cs b/src/Tools/Source/RunTests/Options.cs index ecc626feb0aa4..e7334aa250da5 100644 --- a/src/Tools/Source/RunTests/Options.cs +++ b/src/Tools/Source/RunTests/Options.cs @@ -138,10 +138,24 @@ public Options( Architecture = architecture; } + private static string GetArchitecture() => RuntimeInformation.ProcessArchitecture switch + { + System.Runtime.InteropServices.Architecture.Arm => "arm", + System.Runtime.InteropServices.Architecture.Arm64 => "arm64", + System.Runtime.InteropServices.Architecture.Armv6 => "armv6", + System.Runtime.InteropServices.Architecture.LoongArch64 => "loongarch64", + System.Runtime.InteropServices.Architecture.Ppc64le => "ppc64le", + System.Runtime.InteropServices.Architecture.S390x => "s390x", + System.Runtime.InteropServices.Architecture.Wasm => "wasm", + System.Runtime.InteropServices.Architecture.X86 => "x86", + System.Runtime.InteropServices.Architecture.X64 => "x64", + _ => throw new NotImplementedException() + }; + internal static Options? Parse(string[] args) { string? dotnetFilePath = null; - var architecture = "x64"; + var architecture = GetArchitecture(); var includeHtml = false; var testRuntime = TestRuntime.Both; var configuration = "Debug";