Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit ca92bb5

Browse files
authored
Fix xcode paths (#1040)
* Postprocess xcode project to file search paths for different sdks * changelog * Everything is now reflection
1 parent e108330 commit ca92bb5

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- Use the `buildTargetFilter` command line argument to pass in a comma delimited list of build targets to filter for. For example, `+buildTargetFilter win,macos`.
1616
- Added two new GDK packages: `io.improbable.worker.sdk` and `io.improbable.worker.sdk.mobile` which contain the underlying Worker SDK packages for Windows/MacOS/Linux and Android/iOS respectively. [#894](https://github.com/spatialos/gdk-for-unity/pull/894)
1717
- You may now `[Require]` a `WorkerId` in MonoBehaviours. For example: `[Require] private WorkerId workerId;`. [#1016](https://github.com/spatialos/gdk-for-unity/pull/1016)
18-
18+
- iOS builds now perform a post processing step on the XCode project to ensure x86_64 and arm libraries from the SpatialOS Worker SDK are separated. [#1040](https://github.com/spatialos/gdk-for-unity/pull/1040)
1919

2020
### Changed
2121

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using UnityEditor;
7+
using UnityEditor.Build;
8+
using UnityEditor.Callbacks;
9+
10+
namespace Improbable.Gdk.Mobile
11+
{
12+
public static class BuildPostProcessXCode
13+
{
14+
private static Type pbxType;
15+
16+
[PostProcessBuild]
17+
public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
18+
{
19+
if (buildTarget != BuildTarget.iOS)
20+
{
21+
return;
22+
}
23+
24+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
25+
foreach (var assembly in assemblies)
26+
{
27+
var type = assembly.GetTypes().FirstOrDefault(t => t.FullName == "UnityEditor.iOS.Xcode.PBXProject");
28+
if (type != null)
29+
{
30+
pbxType = type;
31+
break;
32+
}
33+
}
34+
35+
// Safety check again API changes
36+
if (pbxType == null)
37+
{
38+
throw new BuildFailedException("Unable to find type UnityEditor.iOS.Xcode.PBXProject");
39+
}
40+
41+
var xcodeObject = Activator.CreateInstance(pbxType);
42+
43+
// Instantiate PBXProject and read from xcode project file.
44+
var projPath = InvokeStaticMethod<string>("GetPBXProjectPath", path);
45+
var xcodeText = File.ReadAllText(projPath);
46+
InvokeMethod(xcodeObject, "ReadFromString", xcodeText);
47+
48+
// Get Target GUIDs
49+
var unityTargetName = InvokeStaticMethod<string>("GetUnityTargetName");
50+
var unityTestTargetName = InvokeStaticMethod<string>("GetUnityTestTargetName");
51+
var targetGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTargetName);
52+
var targetTestingGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTestTargetName);
53+
54+
// Enumerate configGUIDs that need library path patching
55+
var configNames = InvokeMethod<IEnumerable<string>>(xcodeObject, "BuildConfigNames");
56+
var configGUIDs = configNames
57+
.Select(configName => InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetGUID, configName))
58+
.Concat(configNames.Select(configName =>
59+
InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetTestingGUID, configName)));
60+
61+
// Update library paths for each config
62+
foreach (var configGUID in configGUIDs)
63+
{
64+
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID, "LIBRARY_SEARCH_PATHS",
65+
null, new[]
66+
{
67+
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm",
68+
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64"
69+
});
70+
71+
// Add platform specific paths
72+
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID,
73+
"LIBRARY_SEARCH_PATHS[sdk=iphoneos*]", new[]
74+
{
75+
"$(inherited)",
76+
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm"
77+
}, null);
78+
79+
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID,
80+
"LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]",
81+
new[]
82+
{
83+
"$(inherited)",
84+
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64"
85+
}, null);
86+
}
87+
88+
// Save changes to xcode project file
89+
InvokeMethod(xcodeObject, "WriteToFile", projPath);
90+
}
91+
92+
private static T InvokeStaticMethod<T>(string methodName, params object[] parameters)
93+
{
94+
return (T) pbxType.InvokeMember(methodName,
95+
BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, parameters);
96+
}
97+
98+
private static void InvokeStaticMethod(string methodName, params object[] parameters)
99+
{
100+
pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
101+
null, null, parameters);
102+
}
103+
104+
private static T InvokeMethod<T>(object target, string methodName, params object[] parameters)
105+
{
106+
return (T) pbxType.InvokeMember(methodName,
107+
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, target, parameters);
108+
}
109+
110+
private static void InvokeMethod(object target, string methodName, params object[] parameters)
111+
{
112+
pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
113+
null, target, parameters);
114+
}
115+
}
116+
}

workers/unity/Packages/io.improbable.worker.sdk.mobile/BuildPostProcessXCode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Improbable.Worker.Sdk.Mobile",
3+
"references": [],
4+
"optionalUnityReferences": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": false,
13+
"defineConstraints": [],
14+
"versionDefines": []
15+
}

workers/unity/Packages/io.improbable.worker.sdk.mobile/Improbable.Worker.Sdk.Mobile.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)