Skip to content

Commit

Permalink
Add support for shared libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrehn committed Aug 12, 2020
1 parent bebb460 commit 2c3c900
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
14 changes: 12 additions & 2 deletions conan_ue4cli/commands/precompute.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def precompute(manager, argv):
libDir = join(args.dir, 'precomputed', engineVersion, targetID, 'lib')
Utility.truncateDirectory(libDir)

# Create a bin directory for aggregated DLLs under Windows
binDir = join(args.dir, 'precomputed', engineVersion, targetID, 'bin')
Utility.truncateDirectory(binDir)

# Create a data directory for our aggregated data/resource files
dataDir = join(args.dir, 'precomputed', engineVersion, targetID, 'data')
Utility.truncateDirectory(dataDir)
Expand Down Expand Up @@ -116,15 +120,21 @@ def precompute(manager, argv):
print('Copying "{}"...'.format(include))
Utility.copyFileOrDir(include, includeDir)

# Aggregate the static library files from each of the dependency's libraries
# Aggregate library files from each of the dependency's libraries
resolver = LibraryResolver(targetPlatform, dependency['lib_paths'])
for lib in dependency['libs']:
resolved = resolver.resolve(lib)
if resolved is not None:
print('Copying "{}"...'.format(resolved))
Utility.copyFileOrDir(resolved, libDir)
else:
print('Warning: failed to resolve static library file for library name "{}"'.format(lib))
print('Warning: failed to resolve library file for library name "{}"'.format(lib))

# Aggregate DLL files from each of the dependency's binary directories under Windows
for depBinDir in dependency['bin_paths']:
for dll in glob.glob(join(depBinDir, '*.dll')):
print('Copying "{}"...'.format(dll))
Utility.copyFileOrDir(dll, binDir)

# Aggregate the files from each of the dependency's resource directories
for depResourceDir in dependency['res_paths']:
Expand Down
11 changes: 6 additions & 5 deletions conan_ue4cli/common/LibraryResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ def resolve(self, libName):
Attempts to resolve the path to the library file for the specified library name
'''

# Determine the appropriate filename prefix and suffix for the target platform
# Determine the appropriate filename prefix and suffixes for the target platform
prefix = '' if self.platform == 'Windows' else 'lib'
suffix = '.lib' if self.platform == 'Windows' else '.a'
suffixes = ['.lib'] if self.platform == 'Windows' else ['.a', '.dylib', '.so']

# Iterate through each of our search paths and attempt to find the library file
for searchDir in self.searchPaths:
resolved = join(searchDir, prefix + libName + suffix)
if exists(resolved):
return resolved
for suffix in suffixes:
resolved = join(searchDir, prefix + libName + suffix)
if exists(resolved):
return resolved

# Failed to resolve the library file
return None
32 changes: 30 additions & 2 deletions conan_ue4cli/data/boilerplate_templates/v2/Template.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using UnrealBuildTool;
using System.Diagnostics;
using System.Collections.Generic;

//For Tools.DotNETCommon.JsonObject and Tools.DotNETCommon.FileReference
using Tools.DotNETCommon;
Expand Down Expand Up @@ -72,6 +73,21 @@ private void ProcessDependencies(string depsJson, ReadOnlyTargetRules target, st
PublicAdditionalLibraries.Add(libFull);
}

//Ensure any shared libraries are staged alongside the binaries for the plugin
List<string> searchDirs = new List<string>();
searchDirs.AddRange(dep.GetStringArrayField("bin_paths"));
searchDirs.AddRange(dep.GetStringArrayField("lib_paths"));
foreach (string dir in searchDirs)
{
List<string> binaries = new List<string>();
binaries.AddRange(Directory.GetFiles(dir, "*.dll"));
binaries.AddRange(Directory.GetFiles(dir, "*.dylib"));
binaries.AddRange(Directory.GetFiles(dir, "*.so"));
foreach (string binary in binaries) {
RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", Path.GetFileName(binary)), binary, StagedFileType.NonUFS);
}
}

//Copy any data files needed by the package into our staging directory
string[] dataDirs = dep.GetStringArrayField("res_paths");
foreach (string dir in dataDirs)
Expand All @@ -92,23 +108,35 @@ private bool ProcessPrecomputedData(ReadOnlyTargetRules target, string engineVer
string flagsFile = Path.Combine(targetDir, "flags.json");
string includeDir = Path.Combine(targetDir, "include");
string libDir = Path.Combine(targetDir, "lib");
string binDir = Path.Combine(targetDir, "bin");
string dataDir = Path.Combine(targetDir, "data");

//If any of the required files or directories do not exist then we do not have precomputed data
if (!File.Exists(flagsFile) || !Directory.Exists(includeDir) || !Directory.Exists(libDir) || !Directory.Exists(dataDir)) {
if (!File.Exists(flagsFile) || !Directory.Exists(includeDir) || !Directory.Exists(libDir) || !Directory.Exists(binDir) || !Directory.Exists(dataDir)) {
return false;
}

//Add the precomputed include directory to our search paths
PublicIncludePaths.Add(includeDir);

//Link against all static library files in the lib directory
//Link against all static library files (and import libraries for DLLs under Windows) in the lib directory
string libExtension = ((this.IsWindows(target)) ? ".lib" : ".a");
string[] libs = Directory.GetFiles(libDir, "*" + libExtension);
foreach(string lib in libs) {
PublicAdditionalLibraries.Add(lib);
}

//Under non-Windows platforms, link against all shared library files in the lib directory
if (this.IsWindows(target) == false)
{
List<string> sharedLibs = new List<string>();
sharedLibs.AddRange(Directory.GetFiles(libDir, "*.dylib"));
sharedLibs.AddRange(Directory.GetFiles(libDir, "*.so"));
foreach(string lib in sharedLibs) {
PublicAdditionalLibraries.Add(lib);
}
}

//Attempt to parse the JSON file containing any additional flags, modules and system libraries
JsonObject flags = JsonObject.Read(new FileReference(flagsFile));

Expand Down
32 changes: 30 additions & 2 deletions conan_ue4cli/data/boilerplate_templates/v3/Template.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using UnrealBuildTool;
using System.Diagnostics;
using System.Collections.Generic;

//For Tools.DotNETCommon.JsonObject and Tools.DotNETCommon.FileReference
using Tools.DotNETCommon;
Expand Down Expand Up @@ -72,6 +73,21 @@ private void ProcessDependencies(string depsJson, ReadOnlyTargetRules target, st
PublicAdditionalLibraries.Add(libFull);
}

//Ensure any shared libraries are staged alongside the binaries for the plugin
List<string> searchDirs = new List<string>();
searchDirs.AddRange(dep.GetStringArrayField("bin_paths"));
searchDirs.AddRange(dep.GetStringArrayField("lib_paths"));
foreach (string dir in searchDirs)
{
List<string> binaries = new List<string>();
binaries.AddRange(Directory.GetFiles(dir, "*.dll"));
binaries.AddRange(Directory.GetFiles(dir, "*.dylib"));
binaries.AddRange(Directory.GetFiles(dir, "*.so"));
foreach (string binary in binaries) {
RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", Path.GetFileName(binary)), binary, StagedFileType.NonUFS);
}
}

//Copy any data files needed by the package into our staging directory
string[] dataDirs = dep.GetStringArrayField("res_paths");
foreach (string dir in dataDirs)
Expand All @@ -92,23 +108,35 @@ private bool ProcessPrecomputedData(ReadOnlyTargetRules target, string engineVer
string flagsFile = Path.Combine(targetDir, "flags.json");
string includeDir = Path.Combine(targetDir, "include");
string libDir = Path.Combine(targetDir, "lib");
string binDir = Path.Combine(targetDir, "bin");
string dataDir = Path.Combine(targetDir, "data");

//If any of the required files or directories do not exist then we do not have precomputed data
if (!File.Exists(flagsFile) || !Directory.Exists(includeDir) || !Directory.Exists(libDir) || !Directory.Exists(dataDir)) {
if (!File.Exists(flagsFile) || !Directory.Exists(includeDir) || !Directory.Exists(libDir) || !Directory.Exists(binDir) || !Directory.Exists(dataDir)) {
return false;
}

//Add the precomputed include directory to our search paths
PublicIncludePaths.Add(includeDir);

//Link against all static library files in the lib directory
//Link against all static library files (and import libraries for DLLs under Windows) in the lib directory
string libExtension = ((this.IsWindows(target)) ? ".lib" : ".a");
string[] libs = Directory.GetFiles(libDir, "*" + libExtension);
foreach(string lib in libs) {
PublicAdditionalLibraries.Add(lib);
}

//Under non-Windows platforms, link against all shared library files in the lib directory
if (this.IsWindows(target) == false)
{
List<string> sharedLibs = new List<string>();
sharedLibs.AddRange(Directory.GetFiles(libDir, "*.dylib"));
sharedLibs.AddRange(Directory.GetFiles(libDir, "*.so"));
foreach(string lib in sharedLibs) {
PublicAdditionalLibraries.Add(lib);
}
}

//Attempt to parse the JSON file containing any additional flags, modules and system libraries
JsonObject flags = JsonObject.Read(new FileReference(flagsFile));

Expand Down

0 comments on commit 2c3c900

Please sign in to comment.