Skip to content

Commit

Permalink
Support targeting iOS platforms with ILCompiler (#81476)
Browse files Browse the repository at this point in the history
Fixes #80908
  • Loading branch information
ivanpovazan authored Feb 3, 2023
1 parent ff2a378 commit 47071da
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 46 deletions.
49 changes: 25 additions & 24 deletions src/coreclr/tools/Common/CommandLineHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ public static TargetOS GetTargetOS(string token)

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)
Expand All @@ -96,19 +98,18 @@ public static TargetArchitecture GetTargetArchitecture(string token)
_ => 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<string> inputOptions, IEnumerable<string> outputOptions = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,13 @@ public TargetRegisterMap(TargetOS os)
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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mdType.Name is "WeakReference" or "WeakReference`1" &&
flagsEx |= (ushort)EETypeFlagsEx.HasCriticalFinalizerFlag;
}

if (type.Context.Target.IsOSX && IsTrackedReferenceWithFinalizer(type))
if (type.Context.Target.IsOSXLike && IsTrackedReferenceWithFinalizer(type))
{
flagsEx |= (ushort)EETypeFlagsEx.IsTrackedReferenceWithFinalizerFlag;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3073,7 +3073,7 @@ private void ThrowExceptionForHelper(ref CORINFO_HELPER_DESC throwHelper)
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)
Expand Down
11 changes: 8 additions & 3 deletions src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public enum TargetOS
NetBSD,
SunOS,
WebAssembly,
iOS,
iOSSimulator
}

public enum TargetAbi
Expand Down Expand Up @@ -303,13 +305,16 @@ public bool IsWindows
}

/// <summary>
/// Returns True if compiling for OSX
/// Returns True if compiling for OSX family of operating systems.
/// Currently including OSX, iOS and iOSSimulator
/// </summary>
public bool IsOSX
public bool IsOSXLike
{
get
{
return OperatingSystem == TargetOS.OSX;
return OperatingSystem == TargetOS.OSX ||
OperatingSystem == TargetOS.iOS ||
OperatingSystem == TargetOS.iOSSimulator;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ internal static MarshallerKind GetDisabledMarshallerKind(

internal static bool ShouldCheckForPendingException(TargetDetails target, PInvokeMetadata metadata)
{
if (!target.IsOSX)
if (!target.IsOSXLike)
return false;

const string ObjectiveCMsgSend = "objc_msgSend";
Expand All @@ -944,7 +944,7 @@ internal static bool ShouldCheckForPendingException(TargetDetails target, PInvok

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public void EmitCFICodes(int offset)
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
Expand Down Expand Up @@ -792,9 +792,9 @@ public void BuildSymbolDefinitionMap(ObjectNode node, ISymbolDefinitionNode[] de

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('_');
}
Expand Down Expand Up @@ -908,7 +908,7 @@ private bool ShouldShareSymbol(ObjectNode node)
if (_isSingleFileCompilation)
return false;

if (_targetPlatform.OperatingSystem == TargetOS.OSX)
if (_targetPlatform.IsOSXLike)
return false;

if (!(node is ISymbolNode))
Expand Down Expand Up @@ -1250,6 +1250,16 @@ private static string GetLLVMTripleFromTarget(TargetDetails target)
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void EmitExportedMethods()
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()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private IEnumerable<string> ModuleNameVariations(string name)
}
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);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static IEnumerable<Action<HelpContext>> GetExtendedHelp(HelpContext _)
"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());
Expand Down

0 comments on commit 47071da

Please sign in to comment.