Add support for binary modules with native-code only

This commit is contained in:
Wojtek Figat
2021-02-25 13:01:15 +01:00
parent 3da6f9186c
commit b193a7abc4
21 changed files with 123 additions and 27 deletions

View File

@@ -1091,6 +1091,10 @@ namespace Flax.Build.Bindings
private static unsafe void GenerateCSharp(BuildData buildData, IGrouping<string, Module> binaryModule)
{
// Skip generating C# bindings code for native-only modules
if (binaryModule.Any(x => !x.BuildCSharp))
return;
var contents = new StringBuilder();
var binaryModuleName = binaryModule.Key;
var project = Builder.GetModuleProject(binaryModule.First(), buildData);

View File

@@ -218,7 +218,7 @@ namespace Flax.Build.Bindings
using (var writer = new BinaryWriter(stream, Encoding.UTF8))
{
// Version
writer.Write(4);
writer.Write(5);
writer.Write(File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks);
// Build options
@@ -254,7 +254,7 @@ namespace Flax.Build.Bindings
{
// Version
var version = reader.ReadInt32();
if (version != 4)
if (version != 5)
return false;
if (File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks != reader.ReadInt64())
return false;

View File

@@ -1991,6 +1991,7 @@ namespace Flax.Build.Bindings
// Skip generating C++ bindings code for C#-only modules
if (binaryModule.Any(x => !x.BuildNativeCode))
return;
var useCSharp = binaryModule.Any(x => x.BuildCSharp);
var contents = new StringBuilder();
var binaryModuleName = binaryModule.Key;
@@ -2018,8 +2019,8 @@ namespace Flax.Build.Bindings
contents.AppendLine($"#define {binaryModuleNameUpper}_COMPANY \"{project.Company}\"");
contents.AppendLine($"#define {binaryModuleNameUpper}_COPYRIGHT \"{project.Copyright}\"");
contents.AppendLine();
contents.AppendLine("class NativeBinaryModule;");
contents.AppendLine($"extern \"C\" {binaryModuleNameUpper}_API NativeBinaryModule* GetBinaryModule{binaryModuleName}();");
contents.AppendLine("class BinaryModule;");
contents.AppendLine($"extern \"C\" {binaryModuleNameUpper}_API BinaryModule* GetBinaryModule{binaryModuleName}();");
GenerateCppBinaryModuleHeader?.Invoke(buildData, binaryModule, contents);
Utilities.WriteFileIfChanged(binaryModuleHeaderPath, contents.ToString());
@@ -2033,9 +2034,16 @@ namespace Flax.Build.Bindings
contents.AppendLine();
contents.AppendLine($"StaticallyLinkedBinaryModuleInitializer StaticallyLinkedBinaryModule{binaryModuleName}(GetBinaryModule{binaryModuleName});");
contents.AppendLine();
contents.AppendLine($"extern \"C\" NativeBinaryModule* GetBinaryModule{binaryModuleName}()");
contents.AppendLine($"extern \"C\" BinaryModule* GetBinaryModule{binaryModuleName}()");
contents.AppendLine("{");
contents.AppendLine($" static NativeBinaryModule module(\"{binaryModuleName}\", MAssemblyOptions());");
if (useCSharp)
{
contents.AppendLine($" static NativeBinaryModule module(\"{binaryModuleName}\", MAssemblyOptions());");
}
else
{
contents.AppendLine($" static NativeOnlyBinaryModule module(\"{binaryModuleName}\");");
}
contents.AppendLine(" return &module;");
contents.AppendLine("}");
GenerateCppBinaryModuleSource?.Invoke(buildData, binaryModule, contents);

View File

@@ -33,6 +33,7 @@ namespace Flax.Build.Bindings
writer.Write(Module.FilePath);
BindingsGenerator.Write(writer, Module.BinaryModuleName);
writer.Write(Module.BuildNativeCode);
writer.Write(Module.BuildCSharp);
base.Write(writer);
}
@@ -42,7 +43,9 @@ namespace Flax.Build.Bindings
if (reader.ReadString() != Module.Name ||
reader.ReadString() != Module.FilePath ||
BindingsGenerator.Read(reader, Module.BinaryModuleName) != Module.BinaryModuleName ||
reader.ReadBoolean() != Module.BuildNativeCode)
reader.ReadBoolean() != Module.BuildNativeCode ||
reader.ReadBoolean() != Module.BuildCSharp
)
throw new Exception();
base.Read(reader);

View File

@@ -23,6 +23,8 @@ namespace Flax.Build
var buildOptions = buildData.TargetOptions;
foreach (var binaryModule in buildData.BinaryModules)
{
if (binaryModule.All(x => !x.BuildCSharp))
continue;
var binaryModuleName = binaryModule.Key;
using (new ProfileEventScope(binaryModuleName))
{
@@ -71,6 +73,7 @@ namespace Flax.Build
var dependencyModule = buildData.Rules.GetModule(dependencyName);
if (dependencyModule != null &&
!string.IsNullOrEmpty(dependencyModule.BinaryModuleName) &&
dependencyModule.BuildCSharp &&
dependencyModule.BinaryModuleName != binaryModuleName &&
buildData.Modules.TryGetValue(dependencyModule, out var dependencyModuleOptions))
{

View File

@@ -37,6 +37,11 @@ namespace Flax.Build
/// </summary>
public bool BuildNativeCode = true;
/// <summary>
/// True if module has C# code to build. Can be used for native modules without C# bindings nor code.
/// </summary>
public bool BuildCSharp = true;
/// <summary>
/// Initializes a new instance of the <see cref="Module"/> class.
/// </summary>

View File

@@ -831,6 +831,11 @@ namespace Flax.Build
// C#-only binary module
binaryModuleInfo.NativePath = string.Empty;
}
if (!binaryModule.Any(x => x.BuildCSharp))
{
// Skip C#
binaryModuleInfo.ManagedPath = string.Empty;
}
break;
}
case TargetLinkType.Modular:
@@ -850,6 +855,11 @@ namespace Flax.Build
// C#-only binary module
binaryModuleInfo.NativePath = string.Empty;
}
if (!module.BuildCSharp)
{
// Skip C#
binaryModuleInfo.ManagedPath = string.Empty;
}
break;
}
default: throw new ArgumentOutOfRangeException();