Files
UnrealEngineUWP/Engine/Source/Programs/UnrealBuildTool/Platform/Windows/VCSpecificFileAction.cs
henrik karlsson 5f74d114d5 [UBT]
Changed how single files are built. We don't want to invalidate makefile everytime we build single files since it destroys turnaround times. With this change a single file compile can take 2s (+ the actual compile time).

The new behavior injects a special action per module when creating the makefile. These actions can be used to on-the-fly create a proper compile action that follows the rules of the module that the specific file belongs to.. In a normal build these actions are ignored since the logic deciding which actions to build is backtraced from which binaries we want to create.

When a specific file compile is triggered, the logic deciding which files to build search up all these special actions and create a lookup based on which folders the special actions handle. It then try to find the special action that handles the specific file. The matching special action then creates a compile action that can handle that specific file and then queue up the action for execution. If no special action is found it falls back to try to use actions that have this specific file as input (ispc files for example)

Details:
* Removed lots of custom code for "specific files" handling
* Changed so pch (both private and shared) always use definition file. Added #pragma once and change so pch wrapper file include definition file. This made the adaptive path and specific file easy to implement (just disable pch in compile environment and it will just work)
* Added SingleFileAction for both VCToolChain and ClangToolChain. It now works to compile specific headers and cpp files. (It creates wrapper files on the fly to be able to compile all header files (compiling headers directly blow up if there are circular includes)
* Fixed so GenerateClangDatabase mode works with new changes
* Moved the logic that makes sure all (directly) depending cpp files are recompiled when .h are included in the singlefile option

#preflight 63dcc46f78716a01e8069649
#rb joe.kirchoff

[CL 24094027 by henrik karlsson in ue5-main branch]
2023-02-09 04:20:43 -05:00

87 lines
2.9 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.IO;
using UnrealBuildBase;
namespace UnrealBuildTool
{
internal class VcSpecificFileAction : Action, ISpecificFileAction
{
DirectoryReference SourceDir;
DirectoryReference OutputDir;
VCCompileAction BaseAction;
int SingleFileCounter;
internal VcSpecificFileAction(DirectoryReference Source, DirectoryReference Output, VCCompileAction Action) : base(ActionType.Compile)
{
SourceDir = Source;
OutputDir = Output;
BaseAction = Action;
}
public VcSpecificFileAction(BinaryArchiveReader Reader) : base(ActionType.Compile)
{
SourceDir = Reader.ReadCompactDirectoryReference();
OutputDir = Reader.ReadCompactDirectoryReference();
BaseAction = new VCCompileAction(Reader);
}
public new void Write(BinaryArchiveWriter Writer)
{
Writer.WriteCompactDirectoryReference(SourceDir);
Writer.WriteCompactDirectoryReference(OutputDir);
BaseAction.Write(Writer);
}
public DirectoryReference RootDirectory { get => SourceDir; }
public IExternalAction? CreateAction(FileItem SourceFile, ILogger Logger)
{
// If it is a header file we need to wrap it from another file.. because otherwise it will fail when there are circular dependencies
// (There are a lot of those, very few of the .h files in core can be compiled without errors)
if (SourceFile.HasExtension(".h"))
{
FileItem DummyFile = FileItem.GetItemByFileReference(FileReference.Combine(OutputDir, "SingleFile", SourceFile.Name));
Directory.CreateDirectory(DummyFile.Directory.FullName);
File.WriteAllText(DummyFile.FullName, $"#include \"{SourceFile.FullName.Replace('\\', '/')}\"");
SourceFile = DummyFile;
}
else if (!SourceFile.HasExtension(".cpp"))
{
return null;
}
FileItem ResponseFile = FileItem.GetItemByFileReference(FileReference.Combine(OutputDir, $"SingleFile{SingleFileCounter}.rsp"));
FileItem ObjectFile = FileItem.GetItemByFileReference(FileReference.Combine(OutputDir, $"SingleFile{SingleFileCounter}.obj"));
++SingleFileCounter;
VCCompileAction Action = new VCCompileAction(BaseAction);
Action.ObjectFile = ObjectFile;
Action.SourceFile = SourceFile;
FileItem TempFile = FileItem.GetItemByPath(System.IO.Path.GetTempFileName());
Action.ResponseFile = ResponseFile;
List<string> Arguments = Action.GetCompilerArguments(Logger);
System.IO.File.WriteAllLines(ResponseFile.FullName, Arguments);
return Action;
}
}
class VcSpecificFileActionSerializer : ActionSerializerBase<VcSpecificFileAction>
{
/// <inheritdoc/>
public override VcSpecificFileAction Read(BinaryArchiveReader Reader)
{
return new VcSpecificFileAction(Reader);
}
/// <inheritdoc/>
public override void Write(BinaryArchiveWriter Writer, VcSpecificFileAction Action)
{
Action.Write(Writer);
}
}
}