Skip to content

[draft] Update AppleAppBuilder to use dynamic components on iOSSimulator #54241

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

Closed
Closed
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
4 changes: 3 additions & 1 deletion 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.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -35,7 +36,8 @@ public static async Task Main(string[] args)
delegate* unmanaged<void> unmanagedPtr = &OnButtonClick;
ios_register_button_click(unmanagedPtr);
}
const string msg = "Hello World!\n.NET 5.0";
var mi = typeof(System.Reflection.Metadata.AssemblyExtensions).GetMethod ("GetApplyUpdateCapabilities", BindingFlags.Static | BindingFlags.NonPublic);
string msg = string.Format("Hello World!\nC:<{0}>", mi == null ? "null method" : mi.Invoke(null, Array.Empty<object>()));
for (int i = 0; i < msg.Length; i++)
{
// a kind of an animation
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/AppleAppBuilder/AppleAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ public override bool Execute()
generator.EnableRuntimeLogging = EnableRuntimeLogging;
generator.DiagnosticPorts = DiagnosticPorts;

bool preferDylibs = !isDevice;
XcodeProjectPath = generator.GenerateXCode(ProjectName, MainLibraryFileName, assemblerFiles, assemblerFilesToLink,
AppDir, binDir, MonoRuntimeHeaders, !isDevice, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, Optimized, RuntimeComponents, NativeMainSource);
AppDir, binDir, MonoRuntimeHeaders, preferDylibs, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, Optimized, RuntimeComponents, NativeMainSource);

if (BuildAppBundle)
{
Expand Down
7 changes: 7 additions & 0 deletions src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ target_link_libraries(
"-lc++"
"-liconv"
%NativeLibrariesToLink%
"-L.."
"-lmonosgen-2.0"
)

add_custom_command(
TARGET %ProjectName% POST_BUILD
COMMAND install_name_tool -add_rpath @executable_path/. "$<TARGET_FILE:%ProjectName%>"
)
75 changes: 40 additions & 35 deletions src/tasks/AppleAppBuilder/Xcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ public string GenerateXCode(
string? nativeMainSource = null)
{
// bundle everything as resources excluding native files
var excludes = new List<string> { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libmonosgen-2.0.dylib", "libcoreclr.dylib" };
var excludes = new List<string> { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libcoreclr.dylib" };
if (stripDebugSymbols)
{
excludes.Add(".pdb");
}
if (!preferDylibs) {
excludes.Add("libmonosgen-2.0.dylib");
}

string[] resources = Directory.GetFileSystemEntries(workspace, "", SearchOption.TopDirectoryOnly)
.Where(f => !excludes.Any(e => f.EndsWith(e, StringComparison.InvariantCultureIgnoreCase)))
Expand Down Expand Up @@ -110,46 +113,48 @@ public string GenerateXCode(
string toLink = "";

string[] allComponentLibs = Directory.GetFiles(workspace, "libmono-component-*-static.a");
string[] staticComponentStubLibs = Directory.GetFiles(workspace, "libmono-component-*-stub-static.a");
bool staticLinkAllComponents = false;
string[] staticLinkedComponents = Array.Empty<string>();

if (!string.IsNullOrEmpty(runtimeComponents) && runtimeComponents.Equals("*", StringComparison.OrdinalIgnoreCase))
staticLinkAllComponents = true;
else if (!string.IsNullOrEmpty(runtimeComponents))
staticLinkedComponents = runtimeComponents.Split(";");

// by default, component stubs will be linked and depending on how mono runtime has been build,
// stubs can disable or dynamic load components.
foreach (string staticComponentStubLib in staticComponentStubLibs)
{
string componentLibToLink = staticComponentStubLib;
if (staticLinkAllComponents)
{
// static link component.
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
}
else
if (!preferDylibs) {
string[] staticComponentStubLibs = Directory.GetFiles(workspace, "libmono-component-*-stub-static.a");
bool staticLinkAllComponents = false;
string[] staticLinkedComponents = Array.Empty<string>();

if (!string.IsNullOrEmpty(runtimeComponents) && runtimeComponents.Equals("*", StringComparison.OrdinalIgnoreCase))
staticLinkAllComponents = true;
else if (!string.IsNullOrEmpty(runtimeComponents))
staticLinkedComponents = runtimeComponents.Split(";");

// by default, component stubs will be linked and depending on how mono runtime has been build,
// stubs can disable or dynamic load components.
foreach (string staticComponentStubLib in staticComponentStubLibs)
{
foreach (string staticLinkedComponent in staticLinkedComponents)
string componentLibToLink = staticComponentStubLib;
if (staticLinkAllComponents)
{
// static link component.
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
}
else
{
if (componentLibToLink.Contains(staticLinkedComponent, StringComparison.OrdinalIgnoreCase))
foreach (string staticLinkedComponent in staticLinkedComponents)
{
// static link component.
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
break;
if (componentLibToLink.Contains(staticLinkedComponent, StringComparison.OrdinalIgnoreCase))
{
// static link component.
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
break;
}
}
}
}

// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
if (!File.Exists(componentLibToLink))
{
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
componentLibToLink = staticComponentStubLib;
}
// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
if (!File.Exists(componentLibToLink))
{
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
componentLibToLink = staticComponentStubLib;
}

toLink += $" \"-force_load {componentLibToLink}\"{Environment.NewLine}";
toLink += $" \"-force_load {componentLibToLink}\"{Environment.NewLine}";
}
}

string[] dylibs = Directory.GetFiles(workspace, "*.dylib");
Expand All @@ -161,7 +166,7 @@ public string GenerateXCode(

string libName = Path.GetFileNameWithoutExtension(lib);
// libmono must always be statically linked, for other librarires we can use dylibs
bool dylibExists = libName != "libmonosgen-2.0" && dylibs.Any(dylib => Path.GetFileName(dylib) == libName + ".dylib");
bool dylibExists = /*libName != "libmonosgen-2.0" &&*/ dylibs.Any(dylib => Path.GetFileName(dylib) == libName + ".dylib");

if (forceAOT || !(preferDylibs && dylibExists))
{
Expand Down