-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnrealEnginePython.Build.cs
494 lines (446 loc) · 18.2 KB
/
UnrealEnginePython.Build.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.IO;
using System.Collections.Generic;
public class UnrealEnginePython : ModuleRules
{
// leave this string as empty for triggering auto-discovery of python installations...
private string pythonHome = "";
// otherwise specify the path of your python installation
//private string pythonHome = "C:/Program Files/Python36";
// this is an example for Homebrew on Mac
//private string pythonHome = "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/";
// on Linux an include;libs syntax is expected:
//private string pythonHome = "/usr/local/include/python3.6;/usr/local/lib/libpython3.6.so";
private string[] windowsKnownPaths =
{
// "C:/Program Files/Python37",
"C:/Program Files/Python36",
"C:/Program Files/Python35",
"C:/Python27",
"C:/IntelPython35"
};
private string[] macKnownPaths =
{
"/Library/Frameworks/Python.framework/Versions/3.7",
"/Library/Frameworks/Python.framework/Versions/3.6",
"/Library/Frameworks/Python.framework/Versions/3.5",
"/Library/Frameworks/Python.framework/Versions/2.7",
"/System/Library/Frameworks/Python.framework/Versions/3.7",
"/System/Library/Frameworks/Python.framework/Versions/3.6",
"/System/Library/Frameworks/Python.framework/Versions/3.5",
"/System/Library/Frameworks/Python.framework/Versions/2.7"
};
private string[] linuxKnownIncludesPaths =
{
"/usr/local/include/python3.8",
"/usr/local/include/python3.8m",
"/usr/local/include/python3.7",
"/usr/local/include/python3.7m",
"/usr/local/include/python3.6",
"/usr/local/include/python3.6m",
"/usr/local/include/python3.5",
"/usr/local/include/python3.5m",
"/usr/local/include/python2.7",
"/usr/include/python3.8",
"/usr/include/python3.8m",
"/usr/include/python3.7",
"/usr/include/python3.7m",
"/usr/include/python3.6",
"/usr/include/python3.6m",
"/usr/include/python3.5",
"/usr/include/python3.5m",
"/usr/include/python2.7",
};
private string[] linuxKnownLibsPaths =
{
"/usr/local/lib/libpython3.8.so",
"/usr/local/lib/libpython3.8m.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.8.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.8m.so",
"/usr/local/lib/libpython3.7.so",
"/usr/local/lib/libpython3.7m.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.7.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.7m.so",
"/usr/local/lib/libpython3.6.so",
"/usr/local/lib/libpython3.6m.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.6.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.6m.so",
"/usr/local/lib/libpython3.5.so",
"/usr/local/lib/libpython3.5m.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.5.so",
"/usr/local/lib/x86_64-linux-gnu/libpython3.5m.so",
"/usr/local/lib/libpython2.7.so",
"/usr/local/lib/x86_64-linux-gnu/libpython2.7.so",
"/usr/lib/libpython3.8.so",
"/usr/lib/libpython3.8m.so",
"/usr/lib/x86_64-linux-gnu/libpython3.8.so",
"/usr/lib/x86_64-linux-gnu/libpython3.8m.so",
"/usr/lib/libpython3.7.so",
"/usr/lib/libpython3.7m.so",
"/usr/lib/x86_64-linux-gnu/libpython3.7.so",
"/usr/lib/x86_64-linux-gnu/libpython3.7m.so",
"/usr/lib/libpython3.6.so",
"/usr/lib/libpython3.6m.so",
"/usr/lib/x86_64-linux-gnu/libpython3.6.so",
"/usr/lib/x86_64-linux-gnu/libpython3.6m.so",
"/usr/lib/libpython3.5.so",
"/usr/lib/libpython3.5m.so",
"/usr/lib/x86_64-linux-gnu/libpython3.5.so",
"/usr/lib/x86_64-linux-gnu/libpython3.5m.so",
"/usr/lib/libpython2.7.so",
"/usr/lib/x86_64-linux-gnu/libpython2.7.so",
};
#if WITH_FORWARDED_MODULE_RULES_CTOR
public UnrealEnginePython(ReadOnlyTargetRules Target) : base(Target)
#else
public UnrealEnginePython(TargetInfo Target)
#endif
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDefinitions.Add("WITH_UNREALENGINEPYTHON=1"); // fixed
string PluginDirectory = Path.Combine(ModuleDirectory, "..", "..");
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"Sockets",
"Networking"
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"InputCore",
"Slate",
"SlateCore",
"MovieScene",
"LevelSequence",
"HTTP",
"UMG",
"AppFramework",
"RHI",
"Voice",
"RenderCore",
"MovieSceneCapture",
"Landscape",
"Foliage",
"AIModule",
"ApplicationCore"
// ... add private dependencies that you statically link with here ...
}
);
if (Target.bBuildEditor)
{
PrivateDependencyModuleNames.Add("PythonScriptPlugin");
}
#if WITH_FORWARDED_MODULE_RULES_CTOR
BuildVersion Version;
if (BuildVersion.TryRead(BuildVersion.GetDefaultFileName(), out Version))
{
if (Version.MinorVersion >= 18)
{
PrivateDependencyModuleNames.Add("ApplicationCore");
}
}
#endif
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
#if WITH_FORWARDED_MODULE_RULES_CTOR
if (Target.bBuildEditor)
#else
if (UEBuildConfiguration.bBuildEditor)
#endif
{
PrivateDependencyModuleNames.AddRange(new string[]{
"UnrealEd",
"LevelEditor",
"BlueprintGraph",
"Projects",
"Sequencer",
"SequencerWidgets",
"AssetTools",
"LevelSequenceEditor",
"MovieSceneTools",
"MovieSceneTracks",
"CinematicCamera",
"EditorStyle",
"GraphEditor",
"UMGEditor",
"AIGraph",
"RawMesh",
"DesktopWidgets",
"EditorWidgets",
"FBX",
"Persona",
"PropertyEditor",
"LandscapeEditor",
"MaterialEditor"
});
}
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string LibPath = Path.Combine(Target.UEThirdPartySourceDirectory, "Python3/Win64/libs/python39.lib");
PublicSystemLibraryPaths.Add(Path.GetDirectoryName(LibPath));
PrivateDependencyModuleNames.AddRange(
new string[] {
"Python3"
}
);
}
else if (Target.Platform == UnrealTargetPlatform.Mac)
{
string PythonHome = Path.Combine(Target.UEThirdPartySourceDirectory, "Python3/Mac/include");
PublicIncludePaths.Add(PythonHome);
string LibPath = Path.Combine(Target.UEThirdPartyBinariesDirectory, "Python3/Mac/lib/libpython3.9.dylib");
PublicDelayLoadDLLs.Add(LibPath);
}
else if (Target.Platform == UnrealTargetPlatform.Linux)
{
if (pythonHome == "")
{
string includesPath = DiscoverLinuxPythonIncludesPath();
if (includesPath == null)
{
throw new System.Exception("Unable to find Python includes, please add a search path to linuxKnownIncludesPaths");
}
string libsPath = DiscoverLinuxPythonLibsPath();
if (libsPath == null)
{
throw new System.Exception("Unable to find Python libs, please add a search path to linuxKnownLibsPaths");
}
PublicIncludePaths.Add(Path.Combine(ModuleDirectory,"Include"));
PublicIncludePaths.Add(includesPath);
PublicAdditionalLibraries.Add(libsPath);
}
else
{
string[] items = pythonHome.Split(';');
PublicIncludePaths.Add(items[0]);
PublicAdditionalLibraries.Add(items[1]);
}
}
else if (Target.Platform == UnrealTargetPlatform.Android)
{
string PythonAndroidLibsDirectory = Path.Combine(PluginDirectory, "android");
string includePath = Path.Combine(PythonAndroidLibsDirectory, "include", "python3.7m");
PublicIncludePaths.Add(includePath);
PublicSystemLibraryPaths.AddRange(
new string[] {
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a")
});
string[] libraryPaths = new string[] {
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libbz2.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libcrypto.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libcrypto1.1.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libffi.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "liblzma.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libpython3.7m.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "librubicon.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libsqlite3.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libssl.so"),
Path.Combine(PythonAndroidLibsDirectory, "libs", "arm64-v8a", "libssl1.1.so"),
};
foreach (string libraryPath in libraryPaths)
{
PublicAdditionalLibraries.Add(libraryPath);
}
string APLName = "UnrealEnginePython_APL.xml";
string RelAPLPath = Utils.MakePathRelativeTo(ModuleDirectory, Target.RelativeEnginePath);
AdditionalPropertiesForReceipt.Add("AndroidPlugin", Path.Combine(RelAPLPath, APLName));
}
else if (Target.Platform == UnrealTargetPlatform.IOS)
{
string PythonIOSLibsDirectory = Path.Combine(PluginDirectory, "ios");
string includePath = Path.Combine(PythonIOSLibsDirectory, "Python", "Headers");
PublicIncludePaths.Add(includePath);
PublicSystemLibraryPaths.AddRange(
new string[] {
Path.Combine(PythonIOSLibsDirectory, "BZip2"),
Path.Combine(PythonIOSLibsDirectory, "OpenSSL"),
Path.Combine(PythonIOSLibsDirectory, "Python"),
Path.Combine(PythonIOSLibsDirectory, "XZ"),
Path.Combine(PythonIOSLibsDirectory, "Sqlite3"),
});
string[] libraryPaths = new string[] {
Path.Combine(PythonIOSLibsDirectory, "BZip2", "libbzip2.a"),
Path.Combine(PythonIOSLibsDirectory, "OpenSSL", "libOpenSSL.a"),
Path.Combine(PythonIOSLibsDirectory, "Python", "libPython.a"),
Path.Combine(PythonIOSLibsDirectory, "XZ", "libxz.a"),
Path.Combine(PythonIOSLibsDirectory, "Sqlite3", "libsqlite3.tbd"),
};
foreach (string libraryPath in libraryPaths)
{
PublicAdditionalLibraries.Add(libraryPath);
}
}
}
private bool IsPathRelative(string Path)
{
bool IsRooted = Path.StartsWith("\\", System.StringComparison.Ordinal) || // Root of the current directory on Windows. Also covers "\\" for UNC or "network" paths.
Path.StartsWith("/", System.StringComparison.Ordinal) || // Root of the current directory on Windows, root on UNIX-likes.
// Also covers "\\", considering normalization replaces "\\" with "//".
(Path.Length >= 2 && char.IsLetter(Path[0]) && Path[1] == ':'); // Starts with "<DriveLetter>:"
return !IsRooted;
}
private string DiscoverPythonPath(string[] knownPaths, string binaryPath)
{
// insert the PYTHONHOME content as the first known path
List<string> paths = new List<string>(knownPaths);
paths.Insert(0, Path.Combine(ModuleDirectory, "../../Binaries", binaryPath));
string environmentPath = System.Environment.GetEnvironmentVariable("PYTHONHOME");
if (!string.IsNullOrEmpty(environmentPath))
paths.Insert(0, environmentPath);
// look in an alternate custom location
environmentPath = System.Environment.GetEnvironmentVariable("UNREALENGINEPYTHONHOME");
if (!string.IsNullOrEmpty(environmentPath))
paths.Insert(0, environmentPath);
foreach (string path in paths)
{
string actualPath = path;
if (IsPathRelative(actualPath))
{
actualPath = Path.GetFullPath(Path.Combine(ModuleDirectory, actualPath));
}
string headerFile = Path.Combine(actualPath, "include", "Python.h");
if (File.Exists(headerFile))
{
return actualPath;
}
// this is mainly useful for OSX
headerFile = Path.Combine(actualPath, "Headers", "Python.h");
if (File.Exists(headerFile))
{
return actualPath;
}
}
return "";
}
private string DiscoverLinuxPythonIncludesPath()
{
List<string> paths = new List<string>(linuxKnownIncludesPaths);
paths.Insert(0, Path.Combine(ModuleDirectory, "../../Binaries", "Linux", "include"));
foreach (string path in paths)
{
string headerFile = Path.Combine(path, "Python.h");
if (File.Exists(headerFile))
{
return path;
}
}
return null;
}
private string DiscoverLinuxPythonLibsPath()
{
List<string> paths = new List<string>(linuxKnownLibsPaths);
paths.Insert(0, Path.Combine(ModuleDirectory, "../../Binaries", "Linux", "lib"));
paths.Insert(0, Path.Combine(ModuleDirectory, "../../Binaries", "Linux", "lib64"));
foreach (string path in paths)
{
if (File.Exists(path))
{
return path;
}
}
return null;
}
private string GetMacPythonLibFile(string basePath)
{
// first try with python3
for (int i = 9; i >= 0; i--)
{
string fileName = string.Format("libpython3.{0}.dylib", i);
string fullPath = Path.Combine(basePath, "lib", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
fileName = string.Format("libpython3.{0}m.dylib", i);
fullPath = Path.Combine(basePath, "lib", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
// then python2
for (int i = 9; i >= 0; i--)
{
string fileName = string.Format("libpython2.{0}.dylib", i);
string fullPath = Path.Combine(basePath, "lib", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
fileName = string.Format("libpython2.{0}m.dylib", i);
fullPath = Path.Combine(basePath, "lib", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
throw new System.Exception("Invalid Python installation, missing .dylib files");
}
private string GetWindowsPythonLibFile(string basePath)
{
// just for usability, report if the pythonHome is not in the system path
string[] allPaths = System.Environment.GetEnvironmentVariable("PATH").Split(';');
// this will transform the slashes in backslashes...
string checkedPath = Path.GetFullPath(basePath);
if (checkedPath.EndsWith("\\"))
{
checkedPath = checkedPath.Remove(checkedPath.Length - 1);
}
bool found = false;
foreach (string item in allPaths)
{
if (item == checkedPath || item == checkedPath + "\\")
{
found = true;
break;
}
}
if (!found)
{
System.Console.WriteLine("[WARNING] Your Python installation is not in the system PATH environment variable.");
System.Console.WriteLine("[WARNING] Ensure your python paths are set in GlobalConfig (DefaultEngine.ini) so the path can be corrected at runtime.");
}
// first try with python3
for (int i = 9; i >= 0; i--)
{
string fileName = string.Format("python3{0}.lib", i);
string fullPath = Path.Combine(basePath, "libs", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
// then python2
for (int i = 9; i >= 0; i--)
{
string fileName = string.Format("python2{0}.lib", i);
string fullPath = Path.Combine(basePath, "libs", fileName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
throw new System.Exception("Invalid Python installation, missing .lib files");
}
}