// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System.Collections.Generic;
using System.IO;
using Flax.Build.Graph;
using Flax.Build.NativeCpp;
namespace Flax.Build
{
///
/// The target platform compiler types.
///
public enum TargetCompiler
{
///
/// Microsoft C++ (MSVC) C and C++ compiler and linker.
///
MSVC,
///
/// LLVM-based open source compiler.
///
Clang,
}
///
/// The base class for all build toolchains.
///
public abstract class Toolchain
{
///
/// Gets the platform target type.
///
public Platform Platform { get; private set; }
///
/// Gets the platform target architecture.
///
public TargetArchitecture Architecture { get; private set; }
///
/// The default system include paths (for native C++ compilation).
///
public readonly List SystemIncludePaths = new List();
///
/// The default system library paths (for native C++ linking).
///
public readonly List SystemLibraryPaths = new List();
///
/// True it toolset requires the import library (eg. .lib or .a) when linking the binary (shared library or executable file). Otherwise, linking will be performed again the shared library (eg. .dll or .so).
///
public virtual bool UseImportLibraryWhenLinking => false;
///
/// True it toolset generates the import library (eg. .lib or .a) file automatically when linking the binary (shared library or executable file).
///
public virtual bool GeneratesImportLibraryWhenLinking => false;
///
/// Gets the compiler attribute for symbols exported to shared library (dll file).
///
public abstract string DllExport { get; }
///
/// Gets the compiler attribute for symbols imported from shared library (dll file).
///
public abstract string DllImport { get; }
///
/// Gets the compiler type.
///
public abstract TargetCompiler Compiler { get; }
///
/// Initializes a new instance of the class.
///
/// The platform.
/// The target architecture.
protected Toolchain(Platform platform, TargetArchitecture architecture)
{
Platform = platform;
Architecture = architecture;
}
///
/// Prints the information about the toolchain to the log.
///
public abstract void LogInfo();
///
/// Setups the building environment (native C++). Allows to modify compiler and linker options.
///
/// The build options.
public virtual void SetupEnvironment(BuildOptions options)
{
options.CompileEnv.IncludePaths.AddRange(SystemIncludePaths);
options.LinkEnv.LibraryPaths.AddRange(SystemLibraryPaths);
}
///
/// Called before building a target with a given build options. Can be used to inject custom commands into the task graph.
///
/// The task graph.
/// The current build options.
public virtual void PreBuild(TaskGraph graph, BuildOptions options)
{
}
///
/// Called after building a target with a given build options. Can be used to inject custom commands into the task graph.
///
/// The task graph.
/// The current build options.
public virtual void PostBuild(TaskGraph graph, BuildOptions options)
{
}
///
/// Compiles the C++ source files.
///
/// The task graph.
/// The build options with compilation environment.
/// The source files.
/// The output directory path (for object files).
/// The output data.
public abstract CompileOutput CompileCppFiles(TaskGraph graph, BuildOptions options, List sourceFiles, string outputPath);
///
/// Links the compiled object files.
///
/// The task graph.
/// The build options with linking environment.
/// The output file path (result linked file).
public abstract void LinkFiles(TaskGraph graph, BuildOptions options, string outputFilePath);
///
/// C# compilation options container.
///
public struct CSharpOptions
{
public enum ActionTypes
{
MonoCompile,
MonoLink,
GetOutputFiles,
GetPlatformTools,
};
public ActionTypes Action;
public List InputFiles;
public List OutputFiles;
public string AssembliesPath;
public string ClassLibraryPath;
public string PlatformToolsPath;
public bool EnableDebugSymbols;
public bool EnableToolDebug;
}
///
/// Compiles the C# assembly with AOT cross-compiler.
///
/// The options.
/// True if failed, or not supported.
public virtual bool CompileCSharp(ref CSharpOptions options)
{
switch (options.Action)
{
case CSharpOptions.ActionTypes.GetOutputFiles:
foreach (var inputFile in options.InputFiles)
{
if (Configuration.AOTMode == DotNetAOTModes.MonoAOTDynamic)
options.OutputFiles.Add(inputFile + Platform.SharedLibraryFileExtension);
else
options.OutputFiles.Add(Path.Combine(Path.GetDirectoryName(inputFile), Platform.StaticLibraryFilePrefix + Path.GetFileName(inputFile) + Platform.StaticLibraryFileExtension));
}
return false;
case CSharpOptions.ActionTypes.GetPlatformTools:
options.PlatformToolsPath = Path.Combine(Globals.EngineRoot, "Source/Platforms", Platform.Target.ToString(), "Binaries/Tools");
return false;
}
return true;
}
}
}