Skip to content

Commit 732d214

Browse files
author
Eric Miller
committed
Update how frameworks are added to the iOS project
1 parent 10832dc commit 732d214

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// UnityProjectScript.swift
3+
// UnityBuildKitPackageDescription
4+
//
5+
// Copyright (c) 2017 Handsome
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
25+
import Foundation
26+
27+
extension File {
28+
29+
class func unityFrameworksScriptFile(projectName: String, iOSProjectPath: String) -> Data? {
30+
let file = """
31+
using System.Collections;
32+
using System.IO;
33+
using UnityEngine;
34+
using UnityEditor;
35+
using UnityEditor.SceneManagement;
36+
using UnityEngine.SceneManagement;
37+
using UnityEditor.iOS.Xcode;
38+
39+
public class XcodeFrameworks: MonoBehaviour {
40+
41+
private const string iOSProjectRoot = \"\(iOSProjectPath)\";
42+
private const string iOSProjectName = \"\(projectName)\";
43+
private const string PbxFilePath = iOSProjectName + ".xcodeproj/project.pbxproj";
44+
45+
public static void Perform () {
46+
var pbx = new PBXProject();
47+
var pbxPath = Path.Combine(iOSProjectRoot, PbxFilePath);
48+
pbx.ReadFromFile(pbxPath);
49+
50+
var targetGuid = pbx.TargetGuidByName(iOSProjectName);
51+
pbx.AddFrameworkToProject(targetGuid, "GameKit.framework", true);
52+
pbx.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", false);
53+
pbx.AddFrameworkToProject(targetGuid, "AVFoundation.framework", false);
54+
pbx.AddFrameworkToProject(targetGuid, "CoreVideo.framework", false);
55+
pbx.AddFrameworkToProject(targetGuid, "CoreMedia.framework", false);
56+
pbx.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);
57+
pbx.AddFrameworkToProject(targetGuid, "CoreLocation.framework", false);
58+
pbx.AddFrameworkToProject(targetGuid, "MediaPlayer.framework", false);
59+
pbx.AddFrameworkToProject(targetGuid, "CFNetwork.framework", false);
60+
pbx.AddFrameworkToProject(targetGuid, "AudioToolbox.framework", false);
61+
pbx.AddFrameworkToProject(targetGuid, "OpenAL.framework", false);
62+
pbx.AddFrameworkToProject(targetGuid, "QuartzCore.framework", false);
63+
pbx.AddFrameworkToProject(targetGuid, "Foundation.framework", false);
64+
pbx.AddFrameworkToProject(targetGuid, "MediaToolbox.framework", false);
65+
66+
pbx.WriteToFile(pbxPath);
67+
}
68+
}
69+
""".data(using: .utf8)
70+
return file
71+
}
72+
}

Sources/UBKit/Files/Xcode/SpecFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension File {
5151
UNITY_SCRIPTING_BACKEND: il2cpp
5252
UNITY_IOS_EXPORT_PATH: ${SRCROOT}/../Unity/\(projectName)/ios_build
5353
GCC_PREFIX_HEADER: $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch
54-
OTHER_LDFLAGS: -weak-lSystem -weak_framework CoreMotion -weak_framework GameKit -weak_framework iAd -framework CoreGraphics -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework SystemConfiguration -framework CoreLocation -framework MediaPlayer -framework CFNetwork -framework AudioToolbox -framework OpenAL -framework QuartzCore -framework OpenGLES -framework UIKit -framework Foundation -framework MediaToolbox -liconv.2 -liPhone-lib
54+
OTHER_LDFLAGS: -weak-lSystem -liconv.2 -liPhone-lib -weak_framework CoreMotion -weak_framework iAd -framework OpenGLES
5555
HEADER_SEARCH_PATHS: $(UNITY_IOS_EXPORT_PATH)/Classes $(UNITY_IOS_EXPORT_PATH)/Classes/Native $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include
5656
LIBRARY_SEARCH_PATHS: $(UNITY_IOS_EXPORT_PATH)/Libraries $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include
5757
ENABLE_BITCODE: NO

Sources/UBKit/Models/UnityCommandLine.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Foundation
2929
struct UnityCommandLine {
3030
static let buildAction = "iOSBuilder.Perform"
3131
static let refreshAction = "XcodeRefresher.Refresh"
32+
static let frameworksAction = "XcodeFrameworks.Perform"
3233

3334
struct Arguments {
3435
static let projectPath = "-projectPath"

Sources/UBKit/Workers/FileCopier.swift

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class FileCopier {
8080
return unityFilesResult
8181
}
8282

83+
let addFrameworksResult = addXcodeFrameworks()
84+
guard addFrameworksResult == .success else {
85+
return addFrameworksResult
86+
}
87+
8388
return .success
8489
}
8590
}
@@ -113,25 +118,6 @@ private extension FileCopier {
113118
return .success
114119
}
115120

116-
func x() -> Result {
117-
guard let project = project else {
118-
return .failure(UBKitError.invalidXcodeProject("Failed to find project file"))
119-
}
120-
121-
guard let mainTarget = project.pbxproj.objects.nativeTargets.filter({ $0.value.name == config.iOS.projectName }).first else {
122-
return .failure(UBKitError.invalidXcodeProject("Missing main target"))
123-
}
124-
125-
for phase in mainTarget.value.buildPhases {
126-
if phase.starts(with: "RBP_") {
127-
128-
break
129-
}
130-
}
131-
132-
return .success
133-
}
134-
135121
func changeUnityFiles() -> Result {
136122
let mainFilePath = workingPath.appending(config.unity.projectName).appending("/ios_build/Classes/main.mm")
137123
guard fileManager.fileExists(atPath: mainFilePath) else {
@@ -165,12 +151,13 @@ private extension FileCopier {
165151
func addUnityFiles() -> Result {
166152
let semaphore = DispatchSemaphore(value: 0)
167153
var statusCode: Int32 = 999
154+
let projectPath = workingPath.appending(config.unity.projectName).appending("/ios_build")
168155

169156
shell.perform(
170157
config.unity.applicationPath,
171158
UnityCommandLine.Arguments.batchmode,
172159
UnityCommandLine.Arguments.buildPath,
173-
workingPath.appending(config.unity.projectName).appending("/ios_build"),
160+
projectPath,
174161
UnityCommandLine.Arguments.executeMethod,
175162
UnityCommandLine.refreshAction,
176163
UnityCommandLine.Arguments.quit,
@@ -192,6 +179,37 @@ private extension FileCopier {
192179
}
193180
}
194181

182+
func addXcodeFrameworks() -> Result {
183+
let semaphore = DispatchSemaphore(value: 0)
184+
var statusCode: Int32 = 999
185+
let projectPath = workingPath.appending(config.unity.projectName)
186+
187+
shell.perform(
188+
config.unity.applicationPath,
189+
UnityCommandLine.Arguments.batchmode,
190+
UnityCommandLine.Arguments.projectPath,
191+
projectPath,
192+
UnityCommandLine.Arguments.executeMethod,
193+
UnityCommandLine.frameworksAction,
194+
UnityCommandLine.Arguments.quit,
195+
terminationHandler: { (process) in
196+
statusCode = process.terminationStatus
197+
semaphore.signal()
198+
})
199+
200+
let timeout = semaphore.wait(timeout: DispatchTime.now()+60.0)
201+
switch timeout {
202+
case .success:
203+
if statusCode == 0 {
204+
return .success
205+
} else {
206+
return .failure(UBKitError.shellCommand("Initializing Unity Project"))
207+
}
208+
case .timedOut:
209+
return .failure(UBKitError.waitTimedOut)
210+
}
211+
}
212+
195213
func saveProject() -> Result {
196214
guard let project = project else {
197215
return .failure(UBKitError.invalidXcodeProject("Failed to find project file"))

Sources/UBKit/Workers/UnityProject.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,19 @@ private extension UnityProject {
144144
}
145145

146146
guard fileManager.createFile(
147-
atPath: editorFilePath.appending("ProjectScript.cs"),
147+
atPath: editorFilePath.appending("XcodeProjectRefresher.cs"),
148148
contents: File.unityProjectScriptFile(projectName: projectName, iOSProjectPath: config.iOS.projectPath),
149149
attributes: nil) else {
150150
return .failure(UBKitError.unableToCreateFile("Unity Project Script"))
151151
}
152152

153+
guard fileManager.createFile(
154+
atPath: editorFilePath.appending("XcodeFrameworks.cs"),
155+
contents: File.unityFrameworksScriptFile(projectName: projectName, iOSProjectPath: config.iOS.projectPath),
156+
attributes: nil) else {
157+
return .failure(UBKitError.unableToCreateFile("Xcode Frameworks Script"))
158+
}
159+
153160
return .success
154161
}
155162

UnityBuildKit.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
D4C15EC42023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */; };
11+
D4C15EC52023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */; };
1012
OBJ_204 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
1113
OBJ_210 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_78 /* Package.swift */; };
1214
OBJ_216 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_134 /* Package.swift */; };
@@ -428,6 +430,7 @@
428430

429431
/* Begin PBXFileReference section */
430432
"AEXML::AEXML::Product" /* AEXML.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AEXML.framework; sourceTree = BUILT_PRODUCTS_DIR; };
433+
D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnityFrameworksScript.swift; sourceTree = "<group>"; };
431434
"JSONUtilities::JSONUtilities::Product" /* JSONUtilities.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = JSONUtilities.framework; sourceTree = BUILT_PRODUCTS_DIR; };
432435
OBJ_100 /* PBXHeadersBuildPhase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PBXHeadersBuildPhase.swift; sourceTree = "<group>"; };
433436
OBJ_101 /* PBXLegacyTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PBXLegacyTarget.swift; sourceTree = "<group>"; };
@@ -714,6 +717,7 @@
714717
children = (
715718
OBJ_14 /* UnityEditorBuildScript.swift */,
716719
OBJ_15 /* UnityProjectScript.swift */,
720+
D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */,
717721
);
718722
path = Unity;
719723
sourceTree = "<group>";
@@ -1448,6 +1452,7 @@
14481452
buildActionMask = 0;
14491453
files = (
14501454
OBJ_204 /* Package.swift in Sources */,
1455+
D4C15EC42023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */,
14511456
);
14521457
runOnlyForDeploymentPostprocessing = 0;
14531458
};
@@ -1526,6 +1531,7 @@
15261531
OBJ_291 /* SpecFile.swift in Sources */,
15271532
OBJ_292 /* ViewControllerFile.swift in Sources */,
15281533
OBJ_293 /* Config.swift in Sources */,
1534+
D4C15EC52023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */,
15291535
OBJ_294 /* Result.swift in Sources */,
15301536
OBJ_295 /* Shell.swift in Sources */,
15311537
OBJ_296 /* UBKitError.swift in Sources */,

0 commit comments

Comments
 (0)