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

[mono][ios] Drop marshal-ilgen from iOS builds if it is not needed #88903

Merged
merged 9 commits into from
Jul 24, 2023
2 changes: 2 additions & 0 deletions src/mono/msbuild/apple/build/AppleBuild.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
_InitializeCommonProperties;
_BeforeAppleBuild;
_AppleResolveReferences;
_ScanAssembliesDecideLightweightMarshaler;
_ProcessRuntimeComponents;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I think _ProcessRuntimeComponents target could be refactored a bit, especially as it has a single condition _IsLibraryMode on an item group at https://github.com/dotnet/runtime/pull/88903/files#diff-e351a598f31c0a001f31622d9babc297dbcbe5827eabf55ab92700e1839e70f1R109

Proposed changes:

  1. Add a property depending on library mode builds
  <PropertyGroup Condition="'$(_IsLibraryMode)' == 'true'">
   <_ProcessRuntimeComponentsForLibraryMode>_ProcessRuntimeComponentsForLibraryMode</_ProcessRuntimeComponentsForLibraryMode>
  </PropertyGroup>
  1. Adjust the AppleBuildDependsOn targets
<AppleBuildDependsOn>
        _InitializeCommonProperties;
        _BeforeAppleBuild;
        _AppleResolveReferences;
        _ScanAssembliesDecideLightweightMarshaler;
        $(_ProcessRuntimeComponentsForLibraryMode);
        _AppleAotCompile;
        ...
  1. Rename the _ProcessRuntimeComponents target into _ProcessRuntimeComponentsForLibraryMode
  2. Remove the condition on the item group on line: https://github.com/dotnet/runtime/pull/88903/files#diff-e351a598f31c0a001f31622d9babc297dbcbe5827eabf55ab92700e1839e70f1R109

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

_AppleAotCompile;
_BuildNativeLibrary;
_AppleGenerateAppBundle;
Expand Down
74 changes: 50 additions & 24 deletions src/mono/msbuild/apple/build/AppleBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<UsingTask Condition="'$(RunAOTCompilation)' == 'true'"
TaskName="ILStrip"
AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />
<UsingTask TaskName="MonoTargetsTasks.MarshalingPInvokeScanner"
AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

<Target Name="_CleanPublish"
BeforeTargets="Build">
Expand Down Expand Up @@ -55,30 +57,6 @@
<ItemGroup>
<_CommonLinkerArgs Condition="'$(_IsLibraryMode)' == 'true' and '$(TargetOS)' != 'tvos' and '$(TargetOS)' != 'tvossimulator'" Include="-framework GSS" />
</ItemGroup>

<PropertyGroup>
<RuntimeComponents Condition="'$(RuntimeComponents)' == ''">marshal-ilgen</RuntimeComponents>
</PropertyGroup>

<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
<!-- add all non stub libs first -->
<!-- if runtimecompoents is empty, exclude -static.a and include -stub-static.a instead -->
<!-- if runtimecomponents is *, we're ok because all -static.a is included -->
<!-- if runtimecomponents is a list, add to items and only pull in -static.a -->

<_UsedComponents
Condition="'$(RuntimeComponents)' != '' and '$(RuntimeComponents)' != '*'"
Include="$(RuntimeComponents)" />

<_RuntimeLibraries
Include="$(AppleBuildDir)\*-stub-static.a" />
<_RuntimeLibraries
Include="$(AppleBuildDir)\*.a"
Exclude="$(AppleBuildDir)\*-static.a" />

<_RuntimeLibraries Remove="$(AppleBuildDir)\libmono-component-%(_UsedComponents.Identity)-stub-static.a" />
<_RuntimeLibraries Include="$(AppleBuildDir)\libmono-component-%(_UsedComponents.Identity)-static.a" />
</ItemGroup>
</Target>

<Target Name="_BeforeAppleBuild">
Expand Down Expand Up @@ -110,6 +88,54 @@
</ItemGroup>
</Target>

<Target Name="_ScanAssembliesDecideLightweightMarshaler" DependsOnTargets="_AppleResolveReferences">
<PropertyGroup Condition="'$(RuntimeComponents)'=='*'">
<ForceAllComponents>true</ForceAllComponents>
</PropertyGroup>

<ItemGroup Condition="'$(ForceAllComponents)'!='true'">
<RuntimeComponentList Include="$(RuntimeComponents)" />
<AssembliesToScan Include="@(_AssembliesToBundleInternal)" />
</ItemGroup>

<MarshalingPInvokeScanner Assemblies ="@(AssembliesToScan)" Condition="'$(ForceAllComponents)'!='true'">
<Output TaskParameter="IncompatibleAssemblies" ItemName="MonoLightweightMarshallerIncompatibleAssemblies" />
</MarshalingPInvokeScanner>

<ItemGroup Condition="'@(MonoLightweightMarshallerIncompatibleAssemblies->Count())' > 0">
<RuntimeComponentList Include="marshal-ilgen" KeepDuplicates="false"/>
</ItemGroup>
<ItemGroup Condition="'@(MonoLightweightMarshallerIncompatibleAssemblies->Count())' == 0">
<RuntimeComponentList Remove="marshal-ilgen" />
</ItemGroup>

<PropertyGroup Condition="'$(ForceAllComponents)'!='true'">
<RuntimeComponents>@(RuntimeComponentList)</RuntimeComponents>
</PropertyGroup>
</Target>

<Target Name="_ProcessRuntimeComponents" DependsOnTargets="_ScanAssembliesDecideLightweightMarshaler" BeforeTargets="_BuildNativeLibrary">
<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
<!-- add all non stub libs first -->
<!-- if runtimecompoents is empty, exclude -static.a and include -stub-static.a instead -->
<!-- if runtimecomponents is *, we're ok because all -static.a is included -->
<!-- if runtimecomponents is a list, add to items and only pull in -static.a -->

<_UsedComponents
Condition="'$(RuntimeComponents)' != '' and '$(RuntimeComponents)' != '*'"
Include="$(RuntimeComponents)" />

<_RuntimeLibraries
Include="$(AppleBuildDir)\*-stub-static.a" />
<_RuntimeLibraries
Include="$(AppleBuildDir)\*.a"
Exclude="$(AppleBuildDir)\*-static.a" />

<_RuntimeLibraries Remove="$(AppleBuildDir)\libmono-component-%(_UsedComponents.Identity)-stub-static.a" />
<_RuntimeLibraries Include="$(AppleBuildDir)\libmono-component-%(_UsedComponents.Identity)-static.a" />
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Target>

<Target Name="_AppleAotCompile"
Condition="'$(RunAOTCompilation)' == 'true'">

Expand Down
20 changes: 17 additions & 3 deletions src/mono/sample/iOS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
Expand All @@ -10,18 +11,31 @@ public static class Program
{
// Defined in main.m
[DllImport("__Internal")]
private static extern void ios_set_text(string value);
unsafe private static extern void ios_set_text(byte* value);

[DllImport("__Internal")]
unsafe private static extern void ios_register_button_click(delegate* unmanaged<void> callback);

private static int counter = 0;

private static void SetText(string txt)
{
byte[] ascii = ASCIIEncoding.ASCII.GetBytes(txt);

unsafe
{
fixed (byte* asciiPtr = ascii)
{
ios_set_text(asciiPtr);
}
}
}

// Called by native code, see main.m
[UnmanagedCallersOnly]
private static void OnButtonClick()
{
ios_set_text("OnButtonClick! #" + counter++);
SetText("OnButtonClick! #" + counter++);
}

#if CI_TEST
Expand All @@ -39,7 +53,7 @@ public static async Task Main(string[] args)
for (int i = 0; i < msg.Length; i++)
{
// a kind of an animation
ios_set_text(msg.Substring(0, i + 1));
SetText(msg.Substring(0, i + 1));
await Task.Delay(100);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mono/sample/iOS/Program.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

<Target Name="BuildAppBundle" AfterTargets="$(BuildAppBundleAfterTargets)" DependsOnTargets="$(BuildAppBundleDependsOnTargets)"/>
<Target Name="_SetAppleGenerateAppBundleProps" Condition="'$(TargetOS)' != 'ios' and '$(ArchiveTests)' != 'true'" BeforeTargets="_AppleGenerateAppBundle">
<PropertyGroup>
<RuntimeComponents>diagnostics_tracing;marshal-ilgen</RuntimeComponents>
</PropertyGroup>
<ItemGroup>
<RuntimeComponents Include="diagnostics_tracing" />
</ItemGroup>
</Target>

<Target Name="RunAppBundle"
Expand Down