Skip to content

Commit

Permalink
Added target platform switch and supported Unreal Engine 4. #81
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Sep 17, 2019
1 parent 231937c commit 57dde7c
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 11 deletions.
11 changes: 10 additions & 1 deletion IL2C.Core/SimpleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public static void Translate(
CodeTextStorage storage,
bool readSymbols,
bool enableBundler,
TargetPlatforms targetPlatform,
DebugInformationOptions debugInformationOptions,
string assemblyPath)
{
logw.Write("IL2C: Preparing assembly: \"{0}\" ...", Path.GetFullPath(assemblyPath));

var translateContext = new TranslateContext(assemblyPath, readSymbols);
var translateContext = new TranslateContext(assemblyPath, readSymbols, targetPlatform);
var preparedFunctions = AssemblyPreparer.Prepare(translateContext);

logw.WriteLine(" done.");
Expand Down Expand Up @@ -63,6 +64,7 @@ public static void TranslateAll(
CodeTextStorage storage,
bool readSymbols,
bool enableBundler,
TargetPlatforms targetPlatform,
DebugInformationOptions debugInformationOptions,
IEnumerable<string> assemblyPaths)
{
Expand All @@ -73,6 +75,7 @@ public static void TranslateAll(
storage,
readSymbols,
enableBundler,
targetPlatform,
debugInformationOptions,
aseemblyPath);
}
Expand All @@ -83,6 +86,7 @@ public static void TranslateAll(
CodeTextStorage storage,
bool readSymbols,
bool enableBundler,
TargetPlatforms targetPlatform,
DebugInformationOptions debugInformationOptions,
params string[] assemblyPaths)
{
Expand All @@ -91,6 +95,7 @@ public static void TranslateAll(
storage,
readSymbols,
enableBundler,
targetPlatform,
debugInformationOptions,
(IEnumerable<string>)assemblyPaths);
}
Expand All @@ -101,6 +106,7 @@ public static void TranslateAll(
bool readSymbols,
bool enableCpp,
bool enableBundler,
TargetPlatforms targetPlatform,
DebugInformationOptions debugInformationOptions,
IEnumerable<string> assemblyPaths)
{
Expand All @@ -117,6 +123,7 @@ public static void TranslateAll(
storage,
readSymbols,
enableBundler,
targetPlatform,
debugInformationOptions,
aseemblyPath);
}
Expand All @@ -128,6 +135,7 @@ public static void TranslateAll(
bool readSymbols,
bool enableCpp,
bool enableBundler,
TargetPlatforms targetPlatform,
DebugInformationOptions debugInformationOptions,
params string[] assemblyPaths)
{
Expand All @@ -137,6 +145,7 @@ public static void TranslateAll(
readSymbols,
enableCpp,
enableBundler,
targetPlatform,
debugInformationOptions,
(IEnumerable<string>)assemblyPaths);
}
Expand Down
15 changes: 12 additions & 3 deletions IL2C.Core/TranslateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

namespace IL2C
{
public enum TargetPlatforms
{
Generic,
UE4 // Unreal Engine 4
}

public sealed class TranslateContext
: IPrepareContext, IExtractContextHost
{
Expand All @@ -49,23 +55,26 @@ public sealed class TranslateContext
#endregion

#region Constructors
public TranslateContext(Assembly assembly, bool readSymbols)
: this(assembly.Location, readSymbols)
public TranslateContext(Assembly assembly, bool readSymbols, TargetPlatforms targetPlatform)
: this(assembly.Location, readSymbols, targetPlatform)
{
}

public TranslateContext(string assemblyPath, bool readSymbols)
public TranslateContext(string assemblyPath, bool readSymbols, TargetPlatforms targetPlatform)
{
var context = new MetadataContext(assemblyPath, readSymbols);
this.MetadataContext = context;
this.Assembly = context.MainAssembly;
this.TargetPlatform = targetPlatform;
}
#endregion

public IMetadataContext MetadataContext { get; }

public IAssemblyInformation Assembly { get; }

public TargetPlatforms TargetPlatform { get; }

#region IPrepareContext
void IPrepareContext.RegisterImportIncludeFile(string includeFileName) =>
importIncludes.Add(includeFileName);
Expand Down
1 change: 1 addition & 0 deletions IL2C.Core/Translators/IExtractContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal interface IExtractContext
{
IMetadataContext MetadataContext { get; }
IAssemblyInformation Assembly { get; }
TargetPlatforms TargetPlatform { get; }

string GetExceptionNestedFrameIndexName();

Expand Down
25 changes: 25 additions & 0 deletions IL2C.Core/Writers/SourceCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ public static string[] WriteSourceCodes(
{
using (var twSource = storage.CreateSourceCodeWriter(targetType.Name))
{
// HACK: Unreal Engine 4 needs include directive with same file name as header extension (ex: foo.c --> foo.h) at first line.
if (extractContext.TargetPlatform == TargetPlatforms.UE4)
{
twSource.WriteLine(
"#include \"{0}.h\" // [16-1] Needs for Unreal Engine 4.",
targetType.Name);
twSource.SplitLine();
}

twSource.WriteLine(
"// [15-2] This is {0} native code translated by IL2C, do not edit.",
assemblyName);
Expand Down Expand Up @@ -417,6 +426,22 @@ public static string[] WriteSourceCodes(

sourceFiles.Add(twSource.RelatedPath);
}

// HACK: Unreal Engine 4 needs include directive with same file name as header extension (ex: foo.c --> foo.h) at first line.
if (extractContext.TargetPlatform == TargetPlatforms.UE4)
{
using (var twUE4Header = storage.CreateHeaderWriter(targetType.Name))
{
twUE4Header.WriteLine(
"// [16-2] This is {0} native code translated by IL2C, do not edit.",
assemblyName);
twUE4Header.WriteLine(
"// It's a dummy header file for helping and using only Unreal Engine 4.",
assemblyName);

twUE4Header.Flush();
}
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions IL2C.Runtime/src/Core/il2c_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void* il2c_unlink_execution_frame__(/* EXECUTION_FRAME__* */ volatile void* pFra
IL2C_THREAD_CONTEXT* pThreadContext = il2c_get_tls_value(g_TlsIndex__);
il2c_assert(pThreadContext != NULL);

// Save retval into temporary reference anchor.
// IMPORTANT: Save retval into temporary reference anchor.
pThreadContext->pTemporaryReferenceAnchor = pReference;

// Touch for lock region.
Expand All @@ -232,9 +232,14 @@ void* il2c_unlink_execution_frame__(/* EXECUTION_FRAME__* */ volatile void* pFra
void* il2c_cleanup_at_return__(void* pReference)
{
IL2C_THREAD_CONTEXT* pThreadContext = il2c_get_tls_value(g_TlsIndex__);
il2c_assert(pThreadContext != NULL);

pThreadContext->pTemporaryReferenceAnchor = pReference;
// The thread context will not allocate if arrives here with arbitrary thread context
// and didn't construct execution frames.
if (il2c_likely__(pThreadContext != NULL))
{
// IMPORTANT: Save retval into temporary reference anchor.
pThreadContext->pTemporaryReferenceAnchor = pReference;
}

return pReference;
}
Expand Down
2 changes: 1 addition & 1 deletion IL2C.Runtime/src/Core/il2c_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ IL2C_THREAD_CONTEXT* il2c_acquire_thread_context__(const char* pFile, int line)
IL2C_THREAD_CONTEXT* il2c_acquire_thread_context__(void)
#endif
{
// First arrived arbitary native thread: Auto attaching managed thread.
// First arrived arbitrary native thread: Auto attaching managed thread.
IL2C_THREAD_CONTEXT* pThreadContext = il2c_get_tls_value(g_TlsIndex__);
if (il2c_unlikely__(pThreadContext == NULL))
{
Expand Down
10 changes: 10 additions & 0 deletions IL2C.Tasks/Translate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public bool EnableBundler
get; set;
}

public string TargetPlatform
{
get; set;
}

public string DebugInformation
{
get; set;
Expand All @@ -68,6 +73,10 @@ public override bool Execute()
var debugInformation = string.IsNullOrWhiteSpace(this.DebugInformation)
? DebugInformationOptions.CommentOnly
: (DebugInformationOptions)Enum.Parse(typeof(DebugInformationOptions), this.DebugInformation);
var targetPlatform = !string.IsNullOrWhiteSpace(this.TargetPlatform) ?
(Enum.TryParse<TargetPlatforms>(this.TargetPlatform, true, out var t) ?
t : TargetPlatforms.Generic) :
TargetPlatforms.Generic;

var logw = new LogWriter(message =>
this.Log.LogMessage(
Expand All @@ -80,6 +89,7 @@ public override bool Execute()
this.ReadSymbols,
this.EnableCpp,
this.EnableBundler,
targetPlatform,
debugInformation,
this.AssemblyPaths.
Select(path => path.ItemSpec.Trim()).
Expand Down
3 changes: 2 additions & 1 deletion IL2C.Tasks/build/IL2C.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
<IL2CReadSymbols Condition="'$(IL2CReadSymbols)' == ''">true</IL2CReadSymbols>
<IL2CEnableCpp Condition="'$(IL2CEnableCpp)' == ''">false</IL2CEnableCpp>
<IL2CEnableBundler Condition="'$(IL2CEnableBundler)' == ''">false</IL2CEnableBundler>
<IL2CTargetPlatform Condition="'$(IL2CTargetPlatform)' == ''">Generic</IL2CTargetPlatform>
<CoreBuildDependsOn>
$(CoreBuildDependsOn);
IL2CBuild
</CoreBuildDependsOn>
</PropertyGroup>
<Target Name="IL2CBuild" Outputs="$(IL2COutputPath)">
<!-- TODO: Can't use @(IL2CAssemblyPaths) -->
<Translate AssemblyPaths="$(IL2CTargetAssemblyPath)" OutputPath="$(IL2COutputPath)" DebugInformation="$(IL2CDebugInformation)" ReadSymbols="$(IL2CReadSymbols)" EnableCpp="$(IL2CEnableCpp)" EnableBundler="$(IL2CEnableBundler)" />
<Translate AssemblyPaths="$(IL2CTargetAssemblyPath)" OutputPath="$(IL2COutputPath)" DebugInformation="$(IL2CDebugInformation)" ReadSymbols="$(IL2CReadSymbols)" EnableCpp="$(IL2CEnableCpp)" EnableBundler="$(IL2CEnableBundler)" TargetPlatform="$(IL2CTargetPlatform)" />
</Target>
</Project>
5 changes: 4 additions & 1 deletion IL2C/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public static int Main(string[] args)
{
var debugInformationOptions = DebugInformationOptions.None;
var readSymbols = true;
var enableBundler = false;
var enableCpp = false;
var enableBundler = false;
var targetPlatform = TargetPlatforms.Generic;
var help = false;

var options = new OptionSet()
Expand All @@ -47,6 +48,7 @@ public static int Main(string[] args)
{ "no-read-symbols", "NO read symbol files", _ => readSymbols = false },
{ "cpp", "Produce C++ extension files (apply extension *.cpp instead *.c, body will not change)", _ => enableCpp = true },
{ "bundler", "Produce bundler source file", _ => enableBundler = true },
{ "target=", "Target platform [generic|ue4]", v => targetPlatform = Enum.TryParse<TargetPlatforms>(v, true, out var t) ? t : TargetPlatforms.Generic },
{ "h|help", "Print this help", _ => help = true },
};

Expand All @@ -67,6 +69,7 @@ public static int Main(string[] args)
readSymbols,
enableCpp,
enableBundler,
targetPlatform,
debugInformationOptions,
assemblyPaths);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/IL2C.Core.Test.Fixture/TestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public static async Task ExecuteTestAsync(TestCaseInformation caseInfo)
// Step 1-1: Create translation context.
var translateContext = new TranslateContext(
caseInfo.Method.DeclaringType.Assembly.Location,
true);
true,
TargetPlatforms.Generic);

// Step 1-2: Prepare target methods.
var targetTypes = new HashSet<string>(
Expand Down

0 comments on commit 57dde7c

Please sign in to comment.