Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Tasks/Common/MetadataKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ internal static class MetadataKeys
public const string IsVersion5 = "IsVersion5";
public const string CreateCompositeImage = "CreateCompositeImage";
public const string PerfmapFormatVersion = "PerfmapFormatVersion";
public const string RequiresNativeLink = "RequiresNativeLink";
public const string NativeLinkerInputPath = "NativeLinkerInputPath";

// Debug symbols
public const string RelatedProperty = "related";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PrepareForReadyToRunCompilation : TaskBase
public bool EmitSymbols { get; set; }
public bool ReadyToRunUseCrossgen2 { get; set; }
public bool Crossgen2Composite { get; set; }
public string Crossgen2ContainerFormat { get; set; }

[Required]
public string OutputPath { get; set; }
Expand Down Expand Up @@ -278,7 +279,21 @@ private void ProcessInputFileList(

var compositeR2RImageRelativePath = MainAssembly.GetMetadata(MetadataKeys.RelativePath);
compositeR2RImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, "r2r" + Path.GetExtension(compositeR2RImageRelativePath));

// For non-PE formats, we may need to do a post-processing step to get the final R2R image
// after running crossgen2. In this case, compositeR2RImageRelativePath is the intermediate file
// produced by crossgen2, and compositeR2RFinalImageRelativePath is the final file to be published
// by any post-crossgen2 linking steps and used at runtime.
var compositeR2RFinalImageRelativePath = compositeR2RImageRelativePath;

if (Crossgen2ContainerFormat == "macho")
{
compositeR2RImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, ".o");
compositeR2RFinalImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, ".dylib");
}

var compositeR2RImage = Path.Combine(OutputPath, compositeR2RImageRelativePath);
var compositeR2RImageFinal = Path.Combine(OutputPath, compositeR2RFinalImageRelativePath);

TaskItem r2rCompilationEntry = new(MainAssembly)
{
Expand Down Expand Up @@ -333,10 +348,17 @@ private void ProcessInputFileList(
// Publish it
TaskItem compositeR2RFileToPublish = new(MainAssembly)
{
ItemSpec = compositeR2RImage
ItemSpec = compositeR2RImageFinal
};
compositeR2RFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec);
compositeR2RFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositeR2RImageRelativePath);
compositeR2RFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositeR2RFinalImageRelativePath);

if (compositeR2RImageFinal != compositeR2RImage)
{
compositeR2RFileToPublish.SetMetadata(MetadataKeys.RequiresNativeLink, "true");
compositeR2RFileToPublish.SetMetadata(MetadataKeys.NativeLinkerInputPath, compositeR2RImage);
}

r2rFilesPublishList.Add(compositeR2RFileToPublish);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/RunReadyToRunCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class RunReadyToRunCompiler : ToolTask
public bool UseCrossgen2 { get; set; }
public string Crossgen2ExtraCommandLineArgs { get; set; }
public ITaskItem[] Crossgen2PgoFiles { get; set; }
public string Crossgen2ContainerFormat { get; set; }

[Output]
public bool WarningsDetected { get; set; }
Expand Down Expand Up @@ -342,6 +343,11 @@ private string GenerateCrossgen2ResponseFile()
}
}

if (!string.IsNullOrEmpty(Crossgen2ContainerFormat))
{
result.AppendLine($"--obj-format:{Crossgen2ContainerFormat}");
}

if (!string.IsNullOrEmpty(Crossgen2ExtraCommandLineArgs))
{
foreach (string extraArg in Crossgen2ExtraCommandLineArgs.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Copyright (c) .NET Foundation. All rights reserved.
<PublishReadyToRunUseRuntimePackOptimizationData Condition="'$(PublishReadyToRunUseRuntimePackOptimizationData)' == ''">true</PublishReadyToRunUseRuntimePackOptimizationData>
<PublishReadyToRunPerfmapFormatVersion Condition="'$(PublishReadyToRunPerfmapFormatVersion)' == ''">1</PublishReadyToRunPerfmapFormatVersion>
<PublishReadyToRunEmitSymbols Condition="'$(PublishReadyToRunEmitSymbols)' == '' and ('$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'tvos' or '$(TargetPlatformIdentifier)' == 'maccatalyst')">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunContainerFormat Condition="'$(PublishReadyToRunContainerFormat)' == ''">pe</PublishReadyToRunContainerFormat>

<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('ios-'))">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('tvos-'))">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('iossimulator-'))">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('tvossimulator-'))">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('maccatalyst-'))">false</PublishReadyToRunEmitSymbols>
<PublishReadyToRunEmitSymbols Condition="$(RuntimeIdentifier.StartsWith('osx-'))">false</PublishReadyToRunEmitSymbols>
</PropertyGroup>

<!--
Expand Down Expand Up @@ -342,9 +350,11 @@ Copyright (c) .NET Foundation. All rights reserved.
Condition="'$(_TargetFrameworkVersionWithoutV)' >= '3.0' And '$(PublishReadyToRun)' == 'true' And '$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
DependsOnTargets="_PrepareForReadyToRunCompilation;
_CreateR2RImages;
_CreateR2RSymbols">
_CreateR2RSymbols;
_LinkReadyToRunMachO">
<ItemGroup>
<R2RTelemetry Include="PublishReadyToRunUseCrossgen2" Value="$(PublishReadyToRunUseCrossgen2)" />
<R2RTelemetry Include="PublishReadyToRunContainerFormat" Value="$(PublishReadyToRunContainerFormat)" />
<R2RTelemetry Include="Crossgen2PackVersion" Value="%(ResolvedCrossgen2Pack.NuGetPackageVersion)" />
<R2RTelemetry Include="CompileListCount" Value="@(_ReadyToRunCompileList->Count())" />
<R2RTelemetry Include="FailedCount" Value="@(_ReadyToRunCompilationFailures->Count())" />
Expand Down Expand Up @@ -422,6 +432,7 @@ Copyright (c) .NET Foundation. All rights reserved.
IncludeSymbolsInSingleFile="$(IncludeSymbolsInSingleFile)"
ReadyToRunUseCrossgen2="$(PublishReadyToRunUseCrossgen2)"
Crossgen2Composite="$(PublishReadyToRunComposite)"
Crossgen2ContainerFormat="$(PublishReadyToRunContainerFormat)"
PublishReadyToRunCompositeExclusions="@(PublishReadyToRunCompositeExclusions)"
PublishReadyToRunCompositeRoots="@(PublishReadyToRunCompositeRoots)">

Expand All @@ -435,6 +446,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<Output TaskParameter="ReadyToRunCompositeUnrootedBuildInput" ItemName="_ReadyToRunCompositeUnrootedBuildInput" />

</PrepareForReadyToRunCompilation>

<ItemGroup>
<_ReadyToRunNativeObjectOutputs Include="@(_ReadyToRunFilesToPublish->WithMetadataValue('RequiresNativeLink', 'true'))" />
<_ReadyToRunNativeObjectOutputs OutputPath="%(Identity)" />
</ItemGroup>
</Target>

<UsingTask Condition="'$(Crossgen2TasksOverriden)' != 'true'" TaskName="ResolveReadyToRunCompilers" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
Expand Down Expand Up @@ -469,6 +485,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Crossgen2Tool="@(Crossgen2Tool)"
UseCrossgen2="$(PublishReadyToRunUseCrossgen2)"
Crossgen2PgoFiles="@(_ReadyToRunPgoFiles)"
Crossgen2ContainerFormat="$(PublishReadyToRunContainerFormat)"
Crossgen2ExtraCommandLineArgs="$(PublishReadyToRunCrossgen2ExtraArgs)"
ImplementationAssemblyReferences="@(_ReadyToRunAssembliesToReference)"
ShowCompilerWarnings="$(PublishReadyToRunShowWarnings)"
Expand Down Expand Up @@ -525,4 +542,132 @@ Copyright (c) .NET Foundation. All rights reserved.
Include="@(_ReadyToRunSymbolsCompileList)" />
</ItemGroup>
</Target>

<Target Name="_FindMachOToolchain">
<PropertyGroup>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('ios-'))">ios</_AppleTargetOS>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('tvos-'))">tvos</_AppleTargetOS>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('iossimulator-'))">iossimulator</_AppleTargetOS>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('tvossimulator-'))">tvossimulator</_AppleTargetOS>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('maccatalyst-'))">maccatalyst</_AppleTargetOS>
<_AppleTargetOS Condition="$(RuntimeIdentifier.StartsWith('osx-'))">osx</_AppleTargetOS>

<_AppleTargetArchitecture Condition="$(RuntimeIdentifier.EndsWith('-x64'))">x86_64</_AppleTargetArchitecture>
<_AppleTargetArchitecture Condition="$(RuntimeIdentifier.EndsWith('-arm64'))">arm64</_AppleTargetArchitecture>
</PropertyGroup>

<PropertyGroup>
<AppleMinOSVersion Condition="'$(AppleMinOSVersion)' == '' and '$(_AppleTargetOS)' == 'osx'">12.0</AppleMinOSVersion>
<AppleMinOSVersion Condition="'$(AppleMinOSVersion)' == '' and '$(_AppleTargetOS)' == 'maccatalyst'">15.0</AppleMinOSVersion>
<AppleMinOSVersion Condition="'$(AppleMinOSVersion)' == '' and ($(_AppleTargetOS.StartsWith('ios')) or $(_AppleTargetOS.StartsWith('tvos')))">12.2</AppleMinOSVersion>

<_AppleSdkName Condition="'$(_AppleTargetOS)' == 'ios'">iphoneos</_AppleSdkName>
<_AppleSdkName Condition="'$(_AppleTargetOS)' == 'iossimulator'">iphonesimulator</_AppleSdkName>
<_AppleSdkName Condition="'$(_AppleTargetOS)' == 'tvos'">appletvos</_AppleSdkName>
<_AppleSdkName Condition="'$(_AppleTargetOS)' == 'tvossimulator'">appletvsimulator</_AppleSdkName>
<_AppleSdkName Condition="'$(_AppleTargetOS)' == 'maccatalyst' or '$(_AppleTargetOS)' == 'osx'">macosx</_AppleSdkName>

<_AppleTripleOS Condition="'$(_AppleTargetOS)' == 'osx'">macos</_AppleTripleOS>
<_AppleTripleOS Condition="'$(_AppleTargetOS)' == 'maccatalyst' or $(_AppleTargetOS.StartsWith('ios'))">ios</_AppleTripleOS>
<_AppleTripleOS Condition="$(_AppleTargetOS.StartsWith('tvos'))">tvos</_AppleTripleOS>

<_AppleTripleAbi Condition="'$(_AppleTargetOS)' == 'ios' or '$(_AppleTargetOS)' == 'tvos'">macho</_AppleTripleAbi>
<_AppleTripleAbi Condition="'$(_AppleTargetOS)' == 'maccatalyst'">macabi</_AppleTripleAbi>
<_AppleTripleAbi Condition="$(_AppleTargetOS.EndsWith('simulator'))">simulator</_AppleTripleAbi>

<TargetTriple Condition="'$(_AppleTripleAbi)' == ''">$(_AppleTargetArchitecture)-apple-$(_AppleTripleOS)$(AppleMinOSVersion)</TargetTriple>
<TargetTriple Condition="'$(_AppleTripleAbi)' != ''">$(_AppleTargetArchitecture)-apple-$(_AppleTripleOS)$(AppleMinOSVersion)-$(_AppleTripleAbi)</TargetTriple>
</PropertyGroup>

<PropertyGroup>
<Xcrun Condition="'$(Xcrun)' == ''">xcrun</Xcrun>
<_WhereXcrun>0</_WhereXcrun>
</PropertyGroup>

<Exec Command="command -v &quot;$(Xcrun)&quot;" IgnoreExitCode="true" StandardOutputImportance="Low">
<Output TaskParameter="ExitCode" PropertyName="_WhereXcrun" />
</Exec>
<Error Condition="'$(_WhereXcrun)' != '0'"
Text="'$(Xcrun)' not found in PATH. Make sure '$(Xcrun)' is available in PATH." />

<Exec Command="&quot;$(Xcrun)&quot; --sdk $(_AppleSdkName) --show-sdk-path" IgnoreExitCode="true" StandardOutputImportance="Low" Condition="'$(SysRoot)' == ''" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="SysRoot" />
</Exec>

<Error Condition="!Exists('$(SysRoot)')"
Text="Apple SDK was not found in: '$(SysRoot)'" />

<Exec Command="&quot;$(Xcrun)&quot; --sdk $(_AppleSdkName) --find clang" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="_AppleClang" />
</Exec>

<Error Condition="!Exists('$(_AppleClang)')"
Text="Apple Clang was not found at: '$(_AppleClang)'" />

<Exec Command="dsymutil --help" IgnoreExitCode="true" StandardOutputImportance="Low" Condition="'$(StripSymbols)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="_DsymUtilOutput" />
</Exec>

<PropertyGroup Condition="'$(StripSymbols)' == 'true' and $(_DsymUtilOutput.Contains('--minimize'))">
<DsymUtilOptions>$(DsymUtilOptions) --minimize</DsymUtilOptions>
</PropertyGroup>

<ItemGroup>
<_MachLinkerArg Include="-gz=zlib" Condition="'$(CompressSymbols)' != 'false'" />
<_MachLinkerArg Include="-isysroot &quot;$(SysRoot)&quot;" Condition="'$(SysRoot)' != ''" />
<_MachLinkerArg Include="--target=$(TargetTriple)" />
<_MachLinkerArg Include="-g" Condition="$(NativeDebugSymbols) == 'true'" />
<_MachLinkerArg Include="-dynamiclib" />
<_MachLinkerArg Include="-Wl,-dead_strip" />
</ItemGroup>

<Exec Command="&quot;$(_AppleClang)&quot; --version" IgnoreExitCode="true" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="Low" ConsoleToMSBuild="true">
<Output TaskParameter="ExitCode" PropertyName="_XcodeVersionStringExitCode" />
<Output TaskParameter="ConsoleOutput" PropertyName="_XcodeVersionString" />
</Exec>

<PropertyGroup Condition="('$(_XcodeVersionStringExitCode)' == '0' or '$(_XcodeVersionStringExitCode)' == '1') and '$(_XcodeVersionString)' != ''">
<_XcodeVersion>$([System.Text.RegularExpressions.Regex]::Match($(_XcodeVersionString), '[1-9]\d*'))</_XcodeVersion>
</PropertyGroup>

<ItemGroup Condition="'$(UseLdClassicXCodeLinker)' != 'false'">
<_MachLinkerArg Condition="'$(UseLdClassicXCodeLinker)' == 'true' or '$(_XcodeVersion)' == '15' or '$(_XcodeVersion)' == '16'" Include="-ld_classic" />
</ItemGroup>

<PropertyGroup Condition="'$(UseLdClassicXCodeLinker)' != 'false'">
<!-- Xcode 16 warns on -ld_classic -->
<_IgnoreLinkerWarnings Condition="'$(_XcodeVersion)' == '16'">true</_IgnoreLinkerWarnings>
</PropertyGroup>
</Target>

<!-- Crossgen2 will produce a Mach-O object file. This target will link it into a dylib that can be consumed by the runtime. -->
<Target Name="_LinkReadyToRunMachO"
DependsOnTargets="_FindMachOToolchain"
Inputs="@(_ReadyToRunNativeObjectOutputs->'%(NativeLinkerInputPath)')"
Outputs="%(_ReadyToRunNativeObjectOutputs.Identity)"
Condition="'$(PublishReadyToRunContainerFormat)' == 'macho'">

<PropertyGroup>
<SharedLibraryInstallName Condition="'$(SharedLibraryInstallName)' == ''">@rpath/%(_ReadyToRunNativeObjectOutputs.Filename)%(_ReadyToRunNativeObjectOutputs.Extension)</SharedLibraryInstallName>
</PropertyGroup>

<ItemGroup>
<_MachLinkerArg Include="-Wl,-install_name,&quot;$(SharedLibraryInstallName)&quot;" />
<_MachLinkerArg Include="%(_ReadyToRunNativeObjectOutputs.NativeLinkerInputPath)" />
<_MachLinkerArg Include="-o %(_ReadyToRunNativeObjectOutputs.Identity)" />
</ItemGroup>

<MakeDir Directories="$([System.IO.Path]::GetDirectoryName(%(_ReadyToRunNativeObjectOutputs.Identity)))" />

<Exec Command="&quot;$(_AppleClang)&quot; @(_MachLinkerArg, ' ')"
IgnoreStandardErrorWarningFormat="$(_IgnoreLinkerWarnings)"/>

<!-- remove executable flag -->
<Exec Command="chmod 644 &quot;%(_ReadyToRunNativeObjectOutputs.Identity)&quot;" />

<Exec Condition="'$(StripSymbols)' == 'true'"
Command="
dsymutil $(DsymUtilOptions) &quot;%(_ReadyToRunNativeObjectOutputs.Identity)&quot; &amp;&amp;
strip -no_code_signature_warning -x &quot;%(_ReadyToRunNativeObjectOutputs.Identity)&quot;" />
</Target>
</Project>
Loading