From 47071da67320985a10f4b70f50f894ab411f4994 Mon Sep 17 00:00:00 2001 From: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com> Date: Fri, 3 Feb 2023 09:13:11 +0100 Subject: [PATCH] Support targeting iOS platforms with ILCompiler (#81476) Fixes https://github.com/dotnet/runtime/issues/80908 --- src/coreclr/tools/Common/CommandLineHelpers.cs | 49 +++++++++++----------- .../Target_X64/TargetRegisterMap.cs | 9 +--- .../Internal/Runtime/EETypeBuilderHelpers.cs | 2 +- .../tools/Common/JitInterface/CorInfoImpl.cs | 2 +- .../Common/TypeSystem/Common/TargetDetails.cs | 11 +++-- .../Common/TypeSystem/Interop/IL/MarshalHelpers.cs | 4 +- .../Compiler/DependencyAnalysis/ObjectWriter.cs | 18 ++++++-- .../Compiler/ExportsFileWriter.cs | 2 +- .../aot/ILCompiler/ConfigurablePInvokePolicy.cs | 2 +- .../tools/aot/ILCompiler/ILCompilerRootCommand.cs | 2 +- 10 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/coreclr/tools/Common/CommandLineHelpers.cs b/src/coreclr/tools/Common/CommandLineHelpers.cs index d1282b4..39161e2 100644 --- a/src/coreclr/tools/Common/CommandLineHelpers.cs +++ b/src/coreclr/tools/Common/CommandLineHelpers.cs @@ -69,17 +69,19 @@ namespace System.CommandLine throw new NotImplementedException(); } - - if (token.Equals("windows", StringComparison.OrdinalIgnoreCase)) - return TargetOS.Windows; - else if (token.Equals("linux", StringComparison.OrdinalIgnoreCase)) - return TargetOS.Linux; - else if (token.Equals("osx", StringComparison.OrdinalIgnoreCase)) - return TargetOS.OSX; - else if (token.Equals("freebsd", StringComparison.OrdinalIgnoreCase)) - return TargetOS.FreeBSD; - - throw new CommandLineException($"Target OS '{token}' is not supported"); + else + { + return token.ToLowerInvariant() switch + { + "windows" => TargetOS.Windows, + "linux" => TargetOS.Linux, + "freebsd" => TargetOS.FreeBSD, + "osx" => TargetOS.OSX, + "ios" => TargetOS.iOS, + "iossimulator" => TargetOS.iOSSimulator, + _ => throw new CommandLineException($"Target OS '{token}' is not supported") + }; + } } public static TargetArchitecture GetTargetArchitecture(string token) @@ -96,19 +98,18 @@ namespace System.CommandLine _ => throw new NotImplementedException() }; } - - if (token.Equals("x86", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.X86; - else if (token.Equals("x64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.X64; - else if (token.Equals("arm", StringComparison.OrdinalIgnoreCase) || token.Equals("armel", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.ARM; - else if (token.Equals("arm64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.ARM64; - else if (token.Equals("loongarch64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.LoongArch64; - - throw new CommandLineException($"Target architecture '{token}' is not supported"); + else + { + return token.ToLowerInvariant() switch + { + "x86" => TargetArchitecture.X86, + "x64" => TargetArchitecture.X64, + "arm" or "armel" => TargetArchitecture.ARM, + "arm64" => TargetArchitecture.ARM64, + "loongarch64" => TargetArchitecture.LoongArch64, + _ => throw new CommandLineException($"Target architecture '{token}' is not supported") + }; + } } public static void MakeReproPackage(string makeReproPath, string outputFilePath, string[] args, ParseResult res, IEnumerable inputOptions, IEnumerable outputOptions = null) diff --git a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs index f377441..3d87424 100644 --- a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs +++ b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs @@ -29,20 +29,13 @@ namespace ILCompiler.DependencyAnalysis.X64 Arg3 = Register.R9; Result = Register.RAX; break; - - case TargetOS.Linux: - case TargetOS.OSX: - case TargetOS.FreeBSD: - case TargetOS.SunOS: - case TargetOS.NetBSD: + default: Arg0 = Register.RDI; Arg1 = Register.RSI; Arg2 = Register.RDX; Arg3 = Register.RCX; Result = Register.RAX; break; - default: - throw new NotImplementedException(); } } } diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 34aa52a..ca426c1 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -113,7 +113,7 @@ namespace Internal.Runtime flagsEx |= (ushort)EETypeFlagsEx.HasCriticalFinalizerFlag; } - if (type.Context.Target.IsOSX && IsTrackedReferenceWithFinalizer(type)) + if (type.Context.Target.IsOSXLike && IsTrackedReferenceWithFinalizer(type)) { flagsEx |= (ushort)EETypeFlagsEx.IsTrackedReferenceWithFinalizerFlag; } diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index df7d878..c092df8 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -3073,7 +3073,7 @@ namespace Internal.JitInterface public static CORINFO_OS TargetToOs(TargetDetails target) { return target.IsWindows ? CORINFO_OS.CORINFO_WINNT : - target.IsOSX ? CORINFO_OS.CORINFO_MACOS : CORINFO_OS.CORINFO_UNIX; + target.IsOSXLike ? CORINFO_OS.CORINFO_MACOS : CORINFO_OS.CORINFO_UNIX; } private void getEEInfo(ref CORINFO_EE_INFO pEEInfoOut) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs index ef77bfa..39a3c20 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs @@ -19,6 +19,8 @@ namespace Internal.TypeSystem NetBSD, SunOS, WebAssembly, + iOS, + iOSSimulator } public enum TargetAbi @@ -303,13 +305,16 @@ namespace Internal.TypeSystem } /// - /// Returns True if compiling for OSX + /// Returns True if compiling for OSX family of operating systems. + /// Currently including OSX, iOS and iOSSimulator /// - public bool IsOSX + public bool IsOSXLike { get { - return OperatingSystem == TargetOS.OSX; + return OperatingSystem == TargetOS.OSX || + OperatingSystem == TargetOS.iOS || + OperatingSystem == TargetOS.iOSSimulator; } } diff --git a/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs b/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs index 6402d5e..cb80143 100644 --- a/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs +++ b/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs @@ -927,7 +927,7 @@ namespace Internal.TypeSystem.Interop internal static bool ShouldCheckForPendingException(TargetDetails target, PInvokeMetadata metadata) { - if (!target.IsOSX) + if (!target.IsOSXLike) return false; const string ObjectiveCMsgSend = "objc_msgSend"; @@ -944,7 +944,7 @@ namespace Internal.TypeSystem.Interop internal static int? GetObjectiveCMessageSendFunction(TargetDetails target, string pinvokeModule, string pinvokeFunction) { - if (!target.IsOSX || pinvokeModule != ObjectiveCLibrary) + if (!target.IsOSXLike || pinvokeModule != ObjectiveCLibrary) return null; #pragma warning disable CA1416 diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs index 6b728cc..120e9a8 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs @@ -718,7 +718,7 @@ namespace ILCompiler.DependencyAnalysis Debug.Assert(false); } - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) { // Emit a symbol for beginning of the frame. This is workaround for ld64 // linker bug which would produce DWARF with incorrect pcStart offsets for @@ -792,9 +792,9 @@ namespace ILCompiler.DependencyAnalysis private void AppendExternCPrefix(Utf8StringBuilder sb) { - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) { - // On OSX, we need to prefix an extra underscore to account for correct linkage of + // On OSX-like systems, we need to prefix an extra underscore to account for correct linkage of // extern "C" functions. sb.Append('_'); } @@ -908,7 +908,7 @@ namespace ILCompiler.DependencyAnalysis if (_isSingleFileCompilation) return false; - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) return false; if (!(node is ISymbolNode)) @@ -1250,6 +1250,16 @@ namespace ILCompiler.DependencyAnalysis sys = "darwin16"; abi = "macho"; break; + case TargetOS.iOS: + vendor = "apple"; + sys = "ios11.0"; + abi = "macho"; + break; + case TargetOS.iOSSimulator: + vendor = "apple"; + sys = "ios11.0"; + abi = "simulator"; + break; case TargetOS.WebAssembly: vendor = "unknown"; sys = "unknown"; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs index 4a09297..9937eca 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs @@ -37,7 +37,7 @@ namespace ILCompiler foreach (var method in _methods) streamWriter.WriteLine($" {method.GetUnmanagedCallersOnlyExportName()}"); } - else if(_context.Target.IsOSX) + else if(_context.Target.IsOSXLike) { foreach (var method in _methods) streamWriter.WriteLine($"_{method.GetUnmanagedCallersOnlyExportName()}"); diff --git a/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs b/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs index f5f634c..78f218a 100644 --- a/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs +++ b/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs @@ -91,7 +91,7 @@ namespace ILCompiler } else { - string suffix = _target.IsOSX ? ".dylib" : ".so"; + string suffix = _target.IsOSXLike ? ".dylib" : ".so"; if (name.EndsWith(suffix, StringComparison.Ordinal)) yield return name.Substring(0, name.Length - suffix.Length); diff --git a/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs b/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs index bfbb1fe..235b744 100644 --- a/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs +++ b/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs @@ -306,7 +306,7 @@ namespace ILCompiler "considered to be input files. If no input files begin with '--' then this option is not necessary.\n"); string[] ValidArchitectures = new string[] { "arm", "arm64", "x86", "x64" }; - string[] ValidOS = new string[] { "windows", "linux", "osx", "freebsd" }; + string[] ValidOS = new string[] { "windows", "linux", "freebsd", "osx", "ios", "iossimulator" }; Console.WriteLine("Valid switches for {0} are: '{1}'. The default value is '{2}'\n", "--targetos", string.Join("', '", ValidOS), Helpers.GetTargetOS(null).ToString().ToLowerInvariant()); -- 2.7.4