Skip to content

Commit

Permalink
[dotnet] Link Mono and Xamarin statically in Mac Catalyst by default. F…
Browse files Browse the repository at this point in the history
…ixes #14686. (#18619)

It's possible to create a provisioning profile for Mac Catalyst that
doesn't allow dylibs in the app. It seems a significant number of people run
into this problem when publishing their apps, so avoid it by linking Mono and
Xamarin statically by default instead.

The downside is that build time might increase a little bit.

An upside however is that the app size might decrease somewhat.

Fixes #14686.
  • Loading branch information
rolfbjarne authored Aug 11, 2023
1 parent 18a8b25 commit 2489540
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,15 @@
<_AOTInputDirectory>$(_IntermediateNativeLibraryDir)aot-input/</_AOTInputDirectory>
<_AOTOutputDirectory>$(_IntermediateNativeLibraryDir)aot-output/</_AOTOutputDirectory>

<!-- default to 'static' for Mac Catalyst to work around https://github.com/xamarin/xamarin-macios/issues/14686 -->
<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == '' And '$(_PlatformName)' == 'MacCatalyst'">static</_LibMonoLinkMode>
<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == '' And ('$(ComputedPlatform)' != 'iPhone' Or '$(_PlatformName)' == 'macOS')">dylib</_LibMonoLinkMode>
<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == ''">static</_LibMonoLinkMode>
<_LibMonoExtension Condition="'$(_LibMonoLinkMode)' == 'dylib'">dylib</_LibMonoExtension>
<_LibMonoExtension Condition="'$(_LibMonoLinkMode)' == 'static'">a</_LibMonoExtension>

<!-- default to 'static' for Mac Catalyst to work around https://github.com/xamarin/xamarin-macios/issues/14686 -->
<_LibXamarinLinkMode Condition="'$(_LibXamarinLinkMode)' == '' And '$(_PlatformName)' == 'MacCatalyst'">static</_LibXamarinLinkMode>
<_LibXamarinLinkMode Condition="'$(_LibXamarinLinkMode)' == '' And '$(ComputedPlatform)' != 'iPhone' And '$(_PlatformName)' != 'macOS'">dylib</_LibXamarinLinkMode>
<_LibXamarinLinkMode Condition="'$(_LibXamarinLinkMode)' == ''">static</_LibXamarinLinkMode>
<_LibXamarinExtension Condition="'$(_LibXamarinLinkMode)' == 'dylib'">dylib</_LibXamarinExtension>
Expand Down
8 changes: 0 additions & 8 deletions tests/dotnet/UnitTests/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,14 +1209,6 @@ public void CustomizedCodeSigning (ApplePlatform platform, string runtimeIdentif
var signedDylibs = new List<string> {
Path.Combine (sharedSupportDir, "app2.app", dylibDir, "lib2.dylib"),
};
if (platform == ApplePlatform.MacCatalyst) {
signedDylibs.Add (Path.Combine (dylibDir, "libSystem.IO.Compression.Native.dylib"));
signedDylibs.Add (Path.Combine (dylibDir, "libSystem.Native.dylib"));
signedDylibs.Add (Path.Combine (dylibDir, "libSystem.Net.Security.Native.dylib"));
signedDylibs.Add (Path.Combine (dylibDir, "libSystem.Security.Cryptography.Native.Apple.dylib"));
signedDylibs.Add (Path.Combine (dylibDir, "libmonosgen-2.0.dylib"));
signedDylibs.Add (Path.Combine (dylibDir, "libxamarin-dotnet-debug.dylib"));
}

foreach (var dylib in signedDylibs) {
var path = Path.Combine (appPath, dylib);
Expand Down
18 changes: 17 additions & 1 deletion tests/introspection/ApiPInvokeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,23 @@ protected void Check (Assembly a)
path = null;
break;
case "libSystem.Native":
path += ".dylib";
var staticallyLinked = false;
#if __MACCATALYST__
// always statically linked
staticallyLinked = true;
#elif __IOS__ || __TVOS__
// statically linked on device
staticallyLinked = Runtime.Arch == Arch.DEVICE;
#elif __MACOS__
// never statically linked (by default)
#else
#error Unknown platform
#endif
if (staticallyLinked) {
path = null;
} else {
path += ".dylib";
}
break;
#endif
case "libc":
Expand Down
3 changes: 3 additions & 0 deletions tools/linker/MonoTouch.Tuner/ListExportedSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ bool ProcessMethod (MethodDefinition method)
Where (v => v.EndsWith (".dylib", StringComparison.OrdinalIgnoreCase) || v.EndsWith (".a", StringComparison.OrdinalIgnoreCase)).
Select (v => Path.GetFileNameWithoutExtension (v)).
Select (v => v.StartsWith ("lib", StringComparison.OrdinalIgnoreCase) ? v.Substring (3) : v).ToHashSet ();
#if !__MACOS__
monoLibraryVariations.Add ("System.Globalization.Native"); // System.Private.CoreLib has P/Invokes pointing to libSystem.Globalization.Native, but they're actually in libmonosgen-2.0
#endif
monoLibraryVariations.UnionWith (monoLibraryVariations.Select (v => "lib" + v).ToArray ());
monoLibraryVariations.UnionWith (monoLibraryVariations.Select (v => v + ".dylib").ToArray ());
// If the P/Invoke points to any of those libraries, then we add it as a P/Invoke symbol.
Expand Down

7 comments on commit 2489540

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.