Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[net10.0] [dotnet] Detect if the 'marshal-ilgen' component is needed. #22254

Draft
wants to merge 1 commit into
base: net10.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<UsingTask TaskName="Xamarin.MacDev.Tasks.LinkNativeCode" AssemblyFile="$(_XamarinTaskAssembly)" />
<UsingTask TaskName="Xamarin.MacDev.Tasks.MergeAppBundles" AssemblyFile="$(_XamarinTaskAssembly)" />
<UsingTask TaskName="Xamarin.MacDev.Tasks.MobileILStrip" AssemblyFile="$(_XamarinTaskAssembly)" />
<UsingTask TaskName="Xamarin.MacDev.Tasks.MobileMarshalingPInvokeScanner" AssemblyFile="$(_XamarinTaskAssembly)" />
<UsingTask TaskName="Xamarin.MacDev.Tasks.MacDevMessage" AssemblyFile="$(_XamarinTaskAssembly)" />

<!-- Project types and how do we distinguish between them
Expand Down Expand Up @@ -453,7 +454,26 @@
</Touch>
</Target>

<Target Name="_ComputeMonoComponents" Condition="'$(UseMonoRuntime)' == 'true' And '$(_LibMonoLinkMode)' == 'static'" BeforeTargets="_MonoSelectRuntimeComponents" DependsOnTargets="_ComputeVariables">
<Target
Name="_MonoDecideMarshalILGenComponent"
Condition="'$(_AppleExcludeMarshalIlgenComponent)' != ''"
DependsOnTargets="_ComputeManagedAssemblyToLink"
>

<MobileMarshalingPInvokeScanner
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
Assemblies="@(ManagedAssemblyToLink)">
<Output TaskParameter="IncompatibleAssemblies" ItemName="_AssembliesThatRequireILGen" />
</MobileMarshalingPInvokeScanner>

<PropertyGroup>
<_AppleExcludeMarshalIlgenComponent Condition="'@(_AssembliesThatRequireILGen->Count())' == 0">true</_AppleExcludeMarshalIlgenComponent>
<_AppleExcludeMarshalIlgenComponent Condition="'@(_AssembliesThatRequireILGen->Count())' > 0">false</_AppleExcludeMarshalIlgenComponent>
</PropertyGroup>
</Target>

<Target Name="_ComputeMonoComponents" Condition="'$(UseMonoRuntime)' == 'true' And '$(_LibMonoLinkMode)' == 'static'" BeforeTargets="_MonoSelectRuntimeComponents" DependsOnTargets="_ComputeVariables;_MonoDecideMarshalILGenComponent">
<!-- https://github.com/dotnet/runtime/blob/main/docs/design/mono/components.md -->
<ItemGroup>
<_MonoComponent Include="hot_reload" Condition="'$(MtouchInterpreter)' != ''" />
Expand Down
51 changes: 51 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/MarshalingPInvokeScanner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;

using Xamarin.Messaging.Build.Client;

#nullable enable

namespace Xamarin.MacDev.Tasks {
public class MobileMarshalingPInvokeScanner : MonoTargetsTasks.MarshalingPInvokeScanner, ITaskCallback {
public string SessionId { get; set; } = string.Empty;

// FIXME: we might want to add a property as an output parameter to indicate whether any assemblies require
// marshaling, because the MarshalingPInvokeScanner task has a list of assemblies, which remote builds might
// want to (completely unnecessarily) copy back to Windows.

public override bool Execute ()
{
if (this.ShouldExecuteRemotely (SessionId))
return new TaskRunner (SessionId, BuildEngine4).RunAsync (this).Result;

var result = base.Execute ();

return result;
}

public bool ShouldCopyToBuildServer (ITaskItem item)
{
// Some assemblies are already on the Mac, and we have a 0-length
// output file on Windows. We don't want to copy these files.
// However, some assemblies have to be copied, because they don't
// already exist on the Mac (typically resource assemblies). So
// filter to assemblies with a non-zero length.

var finfo = new FileInfo (item.ItemSpec);
if (!finfo.Exists || finfo.Length == 0)
return false;

return true;
}

public bool ShouldCreateOutputFile (ITaskItem item) => true;

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied () => Enumerable.Empty<ITaskItem> ();
}
}
Loading