Skip to content

Commit 3f085c7

Browse files
committed
Checkpoint - sample with dynamically loaded components on ios
1 parent 8e3d6cd commit 3f085c7

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

src/mono/sample/iOS/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Reflection;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Runtime.InteropServices;
@@ -35,7 +36,8 @@ public static async Task Main(string[] args)
3536
delegate* unmanaged<void> unmanagedPtr = &OnButtonClick;
3637
ios_register_button_click(unmanagedPtr);
3738
}
38-
const string msg = "Hello World!\n.NET 5.0";
39+
var mi = typeof(System.Reflection.Metadata.AssemblyExtensions).GetMethod ("GetApplyUpdateCapabilities", BindingFlags.Static | BindingFlags.NonPublic);
40+
string msg = string.Format("Hello World!\nC:<{0}>", mi == null ? "null method" : mi.Invoke(null, Array.Empty<object>()));
3941
for (int i = 0; i < msg.Length; i++)
4042
{
4143
// a kind of an animation

src/tasks/AppleAppBuilder/AppleAppBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ public override bool Execute()
231231
generator.EnableRuntimeLogging = EnableRuntimeLogging;
232232
generator.DiagnosticPorts = DiagnosticPorts;
233233

234+
bool preferDylibs = !isDevice;
234235
XcodeProjectPath = generator.GenerateXCode(ProjectName, MainLibraryFileName, assemblerFiles, assemblerFilesToLink,
235-
AppDir, binDir, MonoRuntimeHeaders, !isDevice, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, Optimized, RuntimeComponents, NativeMainSource);
236+
AppDir, binDir, MonoRuntimeHeaders, preferDylibs, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, Optimized, RuntimeComponents, NativeMainSource);
236237

237238
if (BuildAppBundle)
238239
{

src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ target_link_libraries(
6666
"-lc++"
6767
"-liconv"
6868
%NativeLibrariesToLink%
69+
"-L.."
70+
"-lmonosgen-2.0"
71+
)
72+
73+
add_custom_command(
74+
TARGET %ProjectName% POST_BUILD
75+
COMMAND install_name_tool -add_rpath @executable_path/. "$<TARGET_FILE:%ProjectName%>"
6976
)

src/tasks/AppleAppBuilder/Xcode.cs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ public string GenerateXCode(
6161
string? nativeMainSource = null)
6262
{
6363
// bundle everything as resources excluding native files
64-
var excludes = new List<string> { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libmonosgen-2.0.dylib", "libcoreclr.dylib" };
64+
var excludes = new List<string> { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libcoreclr.dylib" };
6565
if (stripDebugSymbols)
6666
{
6767
excludes.Add(".pdb");
6868
}
69+
if (!preferDylibs) {
70+
excludes.Add("libmonosgen-2.0.dylib");
71+
}
6972

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

112115
string[] allComponentLibs = Directory.GetFiles(workspace, "libmono-component-*-static.a");
113-
string[] staticComponentStubLibs = Directory.GetFiles(workspace, "libmono-component-*-stub-static.a");
114-
bool staticLinkAllComponents = false;
115-
string[] staticLinkedComponents = Array.Empty<string>();
116-
117-
if (!string.IsNullOrEmpty(runtimeComponents) && runtimeComponents.Equals("*", StringComparison.OrdinalIgnoreCase))
118-
staticLinkAllComponents = true;
119-
else if (!string.IsNullOrEmpty(runtimeComponents))
120-
staticLinkedComponents = runtimeComponents.Split(";");
121-
122-
// by default, component stubs will be linked and depending on how mono runtime has been build,
123-
// stubs can disable or dynamic load components.
124-
foreach (string staticComponentStubLib in staticComponentStubLibs)
125-
{
126-
string componentLibToLink = staticComponentStubLib;
127-
if (staticLinkAllComponents)
128-
{
129-
// static link component.
130-
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
131-
}
132-
else
116+
if (!preferDylibs) {
117+
string[] staticComponentStubLibs = Directory.GetFiles(workspace, "libmono-component-*-stub-static.a");
118+
bool staticLinkAllComponents = false;
119+
string[] staticLinkedComponents = Array.Empty<string>();
120+
121+
if (!string.IsNullOrEmpty(runtimeComponents) && runtimeComponents.Equals("*", StringComparison.OrdinalIgnoreCase))
122+
staticLinkAllComponents = true;
123+
else if (!string.IsNullOrEmpty(runtimeComponents))
124+
staticLinkedComponents = runtimeComponents.Split(";");
125+
126+
// by default, component stubs will be linked and depending on how mono runtime has been build,
127+
// stubs can disable or dynamic load components.
128+
foreach (string staticComponentStubLib in staticComponentStubLibs)
133129
{
134-
foreach (string staticLinkedComponent in staticLinkedComponents)
130+
string componentLibToLink = staticComponentStubLib;
131+
if (staticLinkAllComponents)
132+
{
133+
// static link component.
134+
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
135+
}
136+
else
135137
{
136-
if (componentLibToLink.Contains(staticLinkedComponent, StringComparison.OrdinalIgnoreCase))
138+
foreach (string staticLinkedComponent in staticLinkedComponents)
137139
{
138-
// static link component.
139-
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
140-
break;
140+
if (componentLibToLink.Contains(staticLinkedComponent, StringComparison.OrdinalIgnoreCase))
141+
{
142+
// static link component.
143+
componentLibToLink = componentLibToLink.Replace("-stub-static.a", "-static.a", StringComparison.OrdinalIgnoreCase);
144+
break;
145+
}
141146
}
142147
}
143-
}
144148

145-
// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
146-
if (!File.Exists(componentLibToLink))
147-
{
148-
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
149-
componentLibToLink = staticComponentStubLib;
150-
}
149+
// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
150+
if (!File.Exists(componentLibToLink))
151+
{
152+
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
153+
componentLibToLink = staticComponentStubLib;
154+
}
151155

152-
toLink += $" \"-force_load {componentLibToLink}\"{Environment.NewLine}";
156+
toLink += $" \"-force_load {componentLibToLink}\"{Environment.NewLine}";
157+
}
153158
}
154159

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

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

166171
if (forceAOT || !(preferDylibs && dylibExists))
167172
{

0 commit comments

Comments
 (0)