forked from 20tab/UnrealEnginePython
-
Notifications
You must be signed in to change notification settings - Fork 16
/
UnrealEnginePython.Build.cs
584 lines (515 loc) · 19.9 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.IO;
using System.Collections.Generic;
//using Tools.DotNETCommon;
public class UnrealEnginePython : ModuleRules
{
// leave this string as empty for triggering auto-discovery of python installations...
private string PythonHome = "";
bool UseThirdPartyPython = true; //embedded
bool AutoAddProjectScriptsInPackaging = true;
//Or use the one included in third party folder
protected string ThirdPartyPythonHome
{
get
{
return Path.GetFullPath(Path.Combine(ThirdPartyPath, PythonType));
}
}
// 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"
//Swap python versions here
private string PythonType = "Python36";
//private string PythonType = "python27";
//private string pythonHome = "/usr/local/include/python3.6;/usr/local/lib/libpython3.6.so";
private string ThirdPartyPath
{
get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
}
private string BinariesPath
{
get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../Binaries/")); }
}
private string ScriptsPath
{
get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../Content/Scripts/")); }
}
public string GetUProjectPath()
{
return Path.Combine(ModuleDirectory, "../../../..");
}
private void CopyToProjectBinaries(string Filepath, ReadOnlyTargetRules Target)
{
//System.Console.WriteLine("uprojectpath is: " + Path.GetFullPath(GetUProjectPath()));
string BinariesDir = Path.Combine(GetUProjectPath(), "Binaries", Target.Platform.ToString());
string Filename = Path.GetFileName(Filepath);
//convert relative path
string FullBinariesDir = Path.GetFullPath(BinariesDir);
if (!Directory.Exists(FullBinariesDir))
{
Directory.CreateDirectory(FullBinariesDir);
}
string FullExistingPath = Path.Combine(FullBinariesDir, Filename);
bool ValidFile = false;
//File exists, check if they're the same
if (File.Exists(FullExistingPath))
{
ValidFile = true;
}
//No valid existing file found, copy new dll
if(!ValidFile)
{
File.Copy(Filepath, Path.Combine(FullBinariesDir, Filename), true);
}
}
public void AddRuntimeDependenciesForCopying(ReadOnlyTargetRules Target)
{
if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))
{
RuntimeDependencies.Add(Path.Combine(ScriptsPath, "..."));
if(AutoAddProjectScriptsInPackaging)
{
RuntimeDependencies.Add("$(ProjectDir)/Content/Scripts/...");
}
//Copy binaries as dependencies
if(UseThirdPartyPython)
{
string PlatformString = Target.Platform.ToString();
//Don't add android stuff so we use a manual method to enum a directory
Tools.DotNETCommon.DirectoryReference BinDir = new Tools.DotNETCommon.DirectoryReference(Path.Combine(BinariesPath, PlatformString, "..."));
if (Tools.DotNETCommon.DirectoryReference.Exists(BinDir))
{
foreach (Tools.DotNETCommon.FileReference File in Tools.DotNETCommon.DirectoryReference.EnumerateFiles(BinDir, "*", SearchOption.AllDirectories))
{
//if (!File.ToString().Contains("android"))
bool bValidFile = !File.ToString().Contains("Lib\\site-packages"); //don't copy site-packages (staging path likely too long)
bValidFile = bValidFile && !File.ToString().Contains("Binaries\\Win64\\UE4Editor"); //don't copy UEEditor dll/pdbs. These are not used
if (bValidFile)
{
RuntimeDependencies.Add(File.ToString());
}
}
}
//Copy the thirdparty dll so ue4 loads it by default
string DLLName = PythonType.ToLower() + ".dll";
string PluginDLLPath = Path.Combine(BinariesPath, PlatformString, DLLName);
CopyToProjectBinaries(PluginDLLPath, Target);
//After it's been copied add it as a dependency so it gets copied on packaging
string DLLPath = Path.GetFullPath(Path.Combine(GetUProjectPath(), "Binaries", PlatformString, DLLName));
RuntimeDependencies.Add(DLLPath);
}//end if thirdparty
}//end windows
}
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.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.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.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.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 enableUnityBuild = System.Environment.GetEnvironmentVariable("UEP_ENABLE_UNITY_BUILD");
bUseUnity = string.IsNullOrEmpty(enableUnityBuild);
PublicIncludePaths.AddRange(
new string[] {
Path.Combine(ModuleDirectory, "Public"),
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
Path.Combine(ModuleDirectory, "Private"),
PythonHome,
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"Sockets",
"Networking",
"Projects"
// ... 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"
// ... add private dependencies that you statically link with here ...
}
);
#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) || (Target.Platform == UnrealTargetPlatform.Win32))
{
if (UseThirdPartyPython)
{
PythonHome = ThirdPartyPythonHome;
System.Console.WriteLine("Using Embedded Python at: " + PythonHome);
PublicIncludePaths.Add(PythonHome);
string libPath = Path.Combine(PythonHome, "Lib", string.Format("{0}.lib", PythonType.ToLower()));
System.Console.WriteLine("full lib path: " + libPath);
PublicLibraryPaths.Add(Path.GetDirectoryName(libPath));
PublicAdditionalLibraries.Add(libPath);
string dllPath = Path.Combine(BinariesPath, "Win64", string.Format("{0}.dll", PythonType.ToLower()));
RuntimeDependencies.Add(dllPath);
AddRuntimeDependenciesForCopying(Target);
}
else if (PythonHome == "")
{
PythonHome = DiscoverPythonPath(windowsKnownPaths, "Win64");
if (PythonHome == "")
{
throw new System.Exception("Unable to find Python installation");
}
System.Console.WriteLine("Using Python at: " + PythonHome);
PublicIncludePaths.Add(PythonHome);
string libPath = GetWindowsPythonLibFile(PythonHome);
PublicLibraryPaths.Add(Path.GetDirectoryName(libPath));
PublicAdditionalLibraries.Add(libPath);
}
}
//other platforms
else
{
if (PythonHome == "")
{
PythonHome = DiscoverPythonPath(macKnownPaths, "Mac");
if (PythonHome == "")
{
throw new System.Exception("Unable to find Python installation");
}
System.Console.WriteLine("Using Python at: " + PythonHome);
PublicIncludePaths.Add(PythonHome);
PublicAdditionalLibraries.Add(Path.Combine(PythonHome, "Lib", string.Format("{0}.lib", PythonType)));
}
System.Console.WriteLine("Using Python at: " + PythonHome);
PublicIncludePaths.Add(PythonHome);
string libPath = GetMacPythonLibFile(PythonHome);
PublicLibraryPaths.Add(Path.GetDirectoryName(libPath));
PublicDelayLoadDLLs.Add(libPath);
}
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(includesPath);
PublicAdditionalLibraries.Add(libsPath);
}
else
{
string[] items = PythonHome.Split(';');
PublicIncludePaths.Add(items[0]);
PublicAdditionalLibraries.Add(items[1]);
}
}
#if WITH_FORWARDED_MODULE_RULES_CTOR
else if (Target.Platform == UnrealTargetPlatform.Android)
{
PublicIncludePaths.Add(System.IO.Path.Combine(ModuleDirectory, "../../android/python35/include"));
PublicAdditionalLibraries.Add(System.IO.Path.Combine(ModuleDirectory, "../../android/armeabi-v7a"));
PublicAdditionalLibraries.Add("python3.5m");
string APLName = "UnrealEnginePython_APL.xml";
string RelAPLPath = Utils.MakePathRelativeTo(ModuleDirectory, Target.RelativeEnginePath);
AdditionalPropertiesForReceipt.Add("AndroidPlugin", Path.Combine(RelAPLPath, APLName));
}
#endif
}
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");
}
}