// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using EpicGames.Core; using UnrealBuildBase; namespace UnrealBuildTool { /// /// A module that is compiled from C++ code. /// class UEBuildModuleCPP : UEBuildModule { /// /// Stores a list of all source files, of different types /// public class InputFileCollection { public readonly List HeaderFiles = new List(); public readonly List ISPCHeaderFiles = new List(); public readonly List IXXFiles = new List(); public readonly List CPPFiles = new List(); public readonly List CFiles = new List(); public readonly List CCFiles = new List(); public readonly List MMFiles = new List(); public readonly List RCFiles = new List(); public readonly List ISPCFiles = new List(); } /// /// The directory for this module's generated code /// public readonly DirectoryReference? GeneratedCodeDirectory; /// /// Set for modules that have generated code /// public bool bAddGeneratedCodeIncludePath; /// /// Paths containing *.gen.cpp files for this module. If this is null then this module doesn't have any generated code. /// public List? GeneratedCppDirectories; /// /// List of invalid include directives. These are buffered up and output before we start compiling. /// public List? InvalidIncludeDirectiveMessages; /// /// Set of source directories referenced during a build /// HashSet? SourceDirectories; protected override void GetReferencedDirectories(HashSet Directories) { base.GetReferencedDirectories(Directories); if(!Rules.bUsePrecompiled) { if(SourceDirectories == null) { throw new BuildException("GetReferencedDirectories() should not be called before building."); } Directories.UnionWith(SourceDirectories); } } /// /// List of whitelisted circular dependencies. Please do NOT add new modules here; refactor to allow the modules to be decoupled instead. /// static readonly KeyValuePair[] WhitelistedCircularDependencies = { new KeyValuePair("AIModule", "AITestSuite"), new KeyValuePair("AnimGraph", "UnrealEd"), new KeyValuePair("AnimGraph", "GraphEditor"), new KeyValuePair("AudioMixer", "NonRealtimeAudioRenderer"), new KeyValuePair("AudioMixer", "SoundFieldRendering"), new KeyValuePair("AudioEditor", "DetailCustomizations"), new KeyValuePair("BlueprintGraph", "KismetCompiler"), new KeyValuePair("BlueprintGraph", "UnrealEd"), new KeyValuePair("BlueprintGraph", "GraphEditor"), new KeyValuePair("BlueprintGraph", "Kismet"), new KeyValuePair("BlueprintGraph", "CinematicCamera"), new KeyValuePair("ConfigEditor", "PropertyEditor"), new KeyValuePair("Documentation", "SourceControl"), new KeyValuePair("Engine", "Landscape"), new KeyValuePair("Engine", "UMG"), new KeyValuePair("Engine", "GameplayTags"), new KeyValuePair("Engine", "MaterialShaderQualitySettings"), new KeyValuePair("Engine", "UnrealEd"), new KeyValuePair("Engine", "AudioMixer"), new KeyValuePair("Engine", "CinematicCamera"), new KeyValuePair("Engine", "CollisionAnalyzer"), new KeyValuePair("Engine", "LogVisualizer"), new KeyValuePair("Engine", "Kismet"), new KeyValuePair("FoliageEdit", "ViewportInteraction"), new KeyValuePair("FoliageEdit", "VREditor"), new KeyValuePair("FunctionalTesting", "UnrealEd"), new KeyValuePair("GameplayAbilitiesEditor", "BlueprintGraph"), new KeyValuePair("GameplayDebugger", "AIModule"), new KeyValuePair("GameplayDebugger", "GameplayTasks"), new KeyValuePair("GameplayTasks", "UnrealEd"), new KeyValuePair("GraphEditor", "Kismet"), new KeyValuePair("HierarchicalLODOutliner", "UnrealEd"), new KeyValuePair("Kismet", "BlueprintGraph"), new KeyValuePair("Kismet", "BlueprintNativeCodeGen"), new KeyValuePair("Kismet", "UMGEditor"), new KeyValuePair("Kismet", "Merge"), new KeyValuePair("KismetWidgets", "BlueprintGraph"), new KeyValuePair("Landscape", "UnrealEd"), new KeyValuePair("Landscape", "MaterialUtilities"), new KeyValuePair("LandscapeEditor", "ViewportInteraction"), new KeyValuePair("LandscapeEditor", "VREditor"), new KeyValuePair("LocalizationDashboard", "LocalizationService"), new KeyValuePair("LocalizationDashboard", "MainFrame"), new KeyValuePair("LocalizationDashboard", "TranslationEditor"), new KeyValuePair("MaterialUtilities", "Landscape"), new KeyValuePair("MovieSceneTools", "Sequencer"), new KeyValuePair("NavigationSystem", "UnrealEd"), new KeyValuePair("PacketHandler", "ReliabilityHandlerComponent"), new KeyValuePair("PIEPreviewDeviceProfileSelector", "UnrealEd"), new KeyValuePair("PixelInspectorModule", "UnrealEd"), new KeyValuePair("Sequencer", "MovieSceneTools"), new KeyValuePair("Sequencer", "ViewportInteraction"), new KeyValuePair("SourceControl", "UnrealEd"), new KeyValuePair("UnrealEd", "AudioEditor"), new KeyValuePair("UnrealEd", "ClothingSystemEditor"), new KeyValuePair("UnrealEd", "Documentation"), new KeyValuePair("UnrealEd", "EditorInteractiveToolsFramework"), new KeyValuePair("UnrealEd", "GraphEditor"), new KeyValuePair("UnrealEd", "InputBindingEditor"), new KeyValuePair("UnrealEd", "Kismet"), new KeyValuePair("UnrealEd", "AudioEditor"), new KeyValuePair("BlueprintGraph", "KismetCompiler"), new KeyValuePair("BlueprintGraph", "UnrealEd"), new KeyValuePair("BlueprintGraph", "GraphEditor"), new KeyValuePair("BlueprintGraph", "Kismet"), new KeyValuePair("BlueprintGraph", "CinematicCamera"), new KeyValuePair("ConfigEditor", "PropertyEditor"), new KeyValuePair("SourceControl", "UnrealEd"), new KeyValuePair("Kismet", "BlueprintGraph"), new KeyValuePair("Kismet", "UMGEditor"), new KeyValuePair("MovieSceneTools", "Sequencer"), new KeyValuePair("Sequencer", "MovieSceneTools"), new KeyValuePair("AIModule", "AITestSuite"), new KeyValuePair("GameplayTasks", "UnrealEd"), new KeyValuePair("AnimGraph", "UnrealEd"), new KeyValuePair("AnimGraph", "GraphEditor"), new KeyValuePair("MaterialUtilities", "Landscape"), new KeyValuePair("HierarchicalLODOutliner", "UnrealEd"), new KeyValuePair("PixelInspectorModule", "UnrealEd"), new KeyValuePair("GameplayAbilitiesEditor", "BlueprintGraph"), new KeyValuePair("UnrealEd", "ViewportInteraction"), new KeyValuePair("UnrealEd", "VREditor"), new KeyValuePair("Sequencer", "ViewportInteraction"), new KeyValuePair("NavigationSystem", "UnrealEd"), new KeyValuePair("UnrealEd", "PluginWarden"), new KeyValuePair("UnrealEd", "PropertyEditor"), new KeyValuePair("UnrealEd", "ToolMenusEditor"), new KeyValuePair("WebBrowser", "WebBrowserTexture"), new KeyValuePair("WindowsMixedRealityHMD", "WindowsMixedRealityHandTracking"), new KeyValuePair("UnrealEd", "MeshPaint"), }; public UEBuildModuleCPP(ModuleRules Rules, DirectoryReference IntermediateDirectory, DirectoryReference? GeneratedCodeDirectory) : base(Rules, IntermediateDirectory) { this.GeneratedCodeDirectory = GeneratedCodeDirectory; foreach (string Def in PublicDefinitions) { Log.TraceVerbose("Compile Env {0}: {1}", Name, Def); } foreach (string Def in Rules.PrivateDefinitions) { Log.TraceVerbose("Compile Env {0}: {1}", Name, Def); } foreach(string CircularlyReferencedModuleName in Rules.CircularlyReferencedDependentModules) { if(CircularlyReferencedModuleName != "BlueprintContext" && !WhitelistedCircularDependencies.Any(x => x.Key == Name && x.Value == CircularlyReferencedModuleName)) { Log.TraceWarning("Found reference between '{0}' and '{1}'. Support for circular references is being phased out; please do not introduce new ones.", Name, CircularlyReferencedModuleName); } } AddDefaultIncludePaths(); } /// /// Determines if a file is part of the given module /// /// Path to the file /// True if the file is part of this module public override bool ContainsFile(FileReference Location) { if (base.ContainsFile(Location)) { return true; } if (GeneratedCodeDirectory != null && Location.IsUnderDirectory(GeneratedCodeDirectory)) { return true; } return false; } /// /// Add the default include paths for this module to its settings /// private void AddDefaultIncludePaths() { // Add the module's parent directory to the public include paths, so other modules may include headers from it explicitly. foreach (DirectoryReference ModuleDir in ModuleDirectories) { PublicIncludePaths.Add(ModuleDir.ParentDirectory!); // Add the base directory to the legacy include paths. LegacyPublicIncludePaths.Add(ModuleDir); // Add the 'classes' directory, if it exists DirectoryReference ClassesDirectory = DirectoryReference.Combine(ModuleDir, "Classes"); if (DirectoryLookupCache.DirectoryExists(ClassesDirectory)) { PublicIncludePaths.Add(ClassesDirectory); } // Add all the public directories DirectoryReference PublicDirectory = DirectoryReference.Combine(ModuleDir, "Public"); if (DirectoryLookupCache.DirectoryExists(PublicDirectory)) { PublicIncludePaths.Add(PublicDirectory); ReadOnlyHashSet ExcludeNames = UEBuildPlatform.GetBuildPlatform(Rules.Target.Platform).GetExcludedFolderNames(); EnumerateLegacyIncludePaths(DirectoryItem.GetItemByDirectoryReference(PublicDirectory), ExcludeNames, LegacyPublicIncludePaths); } // Add the 'internal' directory, if it exists DirectoryReference InternalDirectory = DirectoryReference.Combine(ModuleDir, "Internal"); if (DirectoryLookupCache.DirectoryExists(InternalDirectory)) { InternalIncludePaths.Add(InternalDirectory); } // Add the base private directory for this module DirectoryReference PrivateDirectory = DirectoryReference.Combine(ModuleDir, "Private"); if (DirectoryLookupCache.DirectoryExists(PrivateDirectory)) { PrivateIncludePaths.Add(PrivateDirectory); } } } /// /// Enumerates legacy include paths under a given base directory /// /// The directory to start from. This directory is not added to the output list. /// Set of folder names to exclude from the search. /// List populated with the discovered directories static void EnumerateLegacyIncludePaths(DirectoryItem BaseDirectory, ReadOnlyHashSet ExcludeNames, HashSet LegacyPublicIncludePaths) { foreach(DirectoryItem SubDirectory in BaseDirectory.EnumerateDirectories()) { if(!ExcludeNames.Contains(SubDirectory.Name)) { LegacyPublicIncludePaths.Add(SubDirectory.Location); EnumerateLegacyIncludePaths(SubDirectory, ExcludeNames, LegacyPublicIncludePaths); } } } /// /// Path to the precompiled manifest location /// public virtual FileReference PrecompiledManifestLocation { get { return FileReference.Combine(IntermediateDirectory, String.Format("{0}.precompiled", Name)); } } /// /// Sets up the environment for compiling any module that includes the public interface of this module. /// public override void AddModuleToCompileEnvironment( UEBuildModule? SourceModule, UEBuildBinary? SourceBinary, HashSet IncludePaths, HashSet SystemIncludePaths, HashSet ModuleInterfacePaths, List Definitions, List AdditionalFrameworks, List AdditionalPrerequisites, bool bLegacyPublicIncludePaths ) { // This directory may not exist for this module (or ever exist, if it doesn't contain any generated headers), but we want the project files // to search it so we can pick up generated code definitions after UHT is run for the first time. if(bAddGeneratedCodeIncludePath || (ProjectFileGenerator.bGenerateProjectFiles && GeneratedCodeDirectory != null)) { IncludePaths.Add(GeneratedCodeDirectory!); if (Rules.AdditionalCodeGenDirectories != null) { foreach (string CodeGenDir in Rules.AdditionalCodeGenDirectories) { if (Directory.Exists(CodeGenDir)) { IncludePaths.Add(new DirectoryReference(CodeGenDir)); } } } } ModuleInterfacePaths.Add(UEToolChain.GetModuleInterfaceDir(IntermediateDirectory)); base.AddModuleToCompileEnvironment(SourceModule, SourceBinary, IncludePaths, SystemIncludePaths, ModuleInterfacePaths, Definitions, AdditionalFrameworks, AdditionalPrerequisites, bLegacyPublicIncludePaths); } // UEBuildModule interface. public override List Compile(ReadOnlyTargetRules Target, UEToolChain ToolChain, CppCompileEnvironment BinaryCompileEnvironment, List SpecificFilesToCompile, ISourceFileWorkingSet WorkingSet, IActionGraphBuilder Graph) { //UEBuildPlatform BuildPlatform = UEBuildPlatform.GetBuildPlatform(BinaryCompileEnvironment.Platform); List LinkInputFiles = base.Compile(Target, ToolChain, BinaryCompileEnvironment, SpecificFilesToCompile, WorkingSet, Graph); CppCompileEnvironment ModuleCompileEnvironment = CreateModuleCompileEnvironment(Target, BinaryCompileEnvironment); // If the module is precompiled, read the object files from the manifest if(Rules.bUsePrecompiled && Target.LinkType == TargetLinkType.Monolithic) { if(!FileReference.Exists(PrecompiledManifestLocation)) { throw new BuildException("Missing precompiled manifest for '{0}'. This module was most likely not flagged for being included in a precompiled build - set 'PrecompileForTargets = PrecompileTargetsType.Any;' in {0}.build.cs to override.", Name); } PrecompiledManifest Manifest = PrecompiledManifest.Read(PrecompiledManifestLocation); foreach(FileReference OutputFile in Manifest.OutputFiles) { FileItem ObjectFile = FileItem.GetItemByFileReference(OutputFile); if(!ObjectFile.Exists) { throw new BuildException("Missing object file {0} listed in {1}", OutputFile, PrecompiledManifestLocation); } LinkInputFiles.Add(ObjectFile); } return LinkInputFiles; } // Add all the module source directories to the makefile foreach (DirectoryReference ModuleDirectory in ModuleDirectories) { DirectoryItem ModuleDirectoryItem = DirectoryItem.GetItemByDirectoryReference(ModuleDirectory); Graph.AddSourceDir(ModuleDirectoryItem); } // Find all the input files Dictionary DirectoryToSourceFiles = new Dictionary(); InputFileCollection InputFiles = FindInputFiles(Target.Platform, DirectoryToSourceFiles); foreach (KeyValuePair Pair in DirectoryToSourceFiles) { Graph.AddSourceFiles(Pair.Key, Pair.Value); } // If we're compiling only specific files, strip out anything else. This prevents us clobbering response files for anything we're // not going to build, triggering a larger build than necessary when we do a regular build again. if(SpecificFilesToCompile.Count > 0) { InputFiles.CPPFiles.RemoveAll(x => !SpecificFilesToCompile.Contains(x.Location)); InputFiles.CCFiles.RemoveAll(x => !SpecificFilesToCompile.Contains(x.Location)); InputFiles.CFiles.RemoveAll(x => !SpecificFilesToCompile.Contains(x.Location)); if (InputFiles.CPPFiles.Count == 0 && InputFiles.CCFiles.Count == 0 && InputFiles.CFiles.Count == 0 && !SpecificFilesToCompile.Any(x => ContainsFile(x))) { return new List(); } } // Process all of the header file dependencies for this module CheckFirstIncludeMatchesEachCppFile(Target, ModuleCompileEnvironment, InputFiles.HeaderFiles, InputFiles.CPPFiles); // Should we force a precompiled header to be generated for this module? Usually, we only bother with a // precompiled header if there are at least several source files in the module (after combining them for unity // builds.) But for game modules, it can be convenient to always have a precompiled header to single-file // changes to code is really quick to compile. int MinFilesUsingPrecompiledHeader = Target.MinFilesUsingPrecompiledHeader; if (Rules.MinFilesUsingPrecompiledHeaderOverride != 0) { MinFilesUsingPrecompiledHeader = Rules.MinFilesUsingPrecompiledHeaderOverride; } else if (!Rules.bTreatAsEngineModule && Target.bForcePrecompiledHeaderForGameModules) { // This is a game module with only a small number of source files, so go ahead and force a precompiled header // to be generated to make incremental changes to source files as fast as possible for small projects. MinFilesUsingPrecompiledHeader = 1; } // Engine modules will always use unity build mode unless MinSourceFilesForUnityBuildOverride is specified in // the module rules file. By default, game modules only use unity of they have enough source files for that // to be worthwhile. If you have a lot of small game modules, consider specifying MinSourceFilesForUnityBuildOverride=0 // in the modules that you don't typically iterate on source files in very frequently. int MinSourceFilesForUnityBuild = 2; if (Rules.MinSourceFilesForUnityBuildOverride != 0) { MinSourceFilesForUnityBuild = Rules.MinSourceFilesForUnityBuildOverride; } else if (Target.ProjectFile != null && RulesFile.IsUnderDirectory(DirectoryReference.Combine(Target.ProjectFile.Directory, "Source"))) { // Game modules with only a small number of source files are usually better off having faster iteration times // on single source file changes, so we forcibly disable unity build for those modules MinSourceFilesForUnityBuild = Target.MinGameModuleSourceFilesForUnityBuild; } // Should we use unity build mode for this module? bool bModuleUsesUnityBuild = false; if (Target.bUseUnityBuild || Target.bForceUnityBuild) { if (Target.bForceUnityBuild) { Log.TraceVerbose("Module '{0}' using unity build mode (bForceUnityBuild enabled for this module)", this.Name); bModuleUsesUnityBuild = true; } else if (!Rules.bUseUnity) { Log.TraceVerbose("Module '{0}' not using unity build mode (bUseUnity disabled for this module)", this.Name); bModuleUsesUnityBuild = false; } else if (InputFiles.CPPFiles.Count < MinSourceFilesForUnityBuild) { Log.TraceVerbose("Module '{0}' not using unity build mode (module with fewer than {1} source files)", this.Name, MinSourceFilesForUnityBuild); bModuleUsesUnityBuild = false; } else { Log.TraceVerbose("Module '{0}' using unity build mode", this.Name); bModuleUsesUnityBuild = true; } } else { Log.TraceVerbose("Module '{0}' not using unity build mode", this.Name); } // Set up the environment with which to compile the CPP files CppCompileEnvironment CompileEnvironment = ModuleCompileEnvironment; // Generate ISPC headers first so C++ can consume them if (InputFiles.ISPCFiles.Count > 0) { CreateHeadersForISPC(ToolChain, CompileEnvironment, InputFiles.ISPCFiles, IntermediateDirectory, Graph); } // Compile any module interfaces if (InputFiles.IXXFiles.Count > 0 && Target.bEnableCppModules) { CPPOutput ModuleOutput = ToolChain.CompileCPPFiles(CompileEnvironment, InputFiles.IXXFiles, IntermediateDirectory, Name, Graph); LinkInputFiles.AddRange(ModuleOutput.ObjectFiles); CompileEnvironment.AdditionalPrerequisites.AddRange(ModuleOutput.CompiledModuleInterfaces); } // Configure the precompiled headers for this module CompileEnvironment = SetupPrecompiledHeaders(Target, ToolChain, CompileEnvironment, LinkInputFiles, Graph); // Write all the definitions to a separate file CreateHeaderForDefinitions(CompileEnvironment, IntermediateDirectory, null, Graph); // Mapping of source file to unity file. We output this to intermediate directories for other tools (eg. live coding) to use. Dictionary SourceFileToUnityFile = new Dictionary(); // Compile CPP files if (bModuleUsesUnityBuild) { Unity.GenerateUnityCPPs(Target, InputFiles.CPPFiles, InputFiles.HeaderFiles, CompileEnvironment, WorkingSet, Rules.ShortName ?? Name, IntermediateDirectory, Graph, SourceFileToUnityFile, out List NormalFiles, out List AdaptiveFiles); LinkInputFiles.AddRange(CompileFilesWithToolChain(Target, ToolChain, CompileEnvironment, ModuleCompileEnvironment, NormalFiles, AdaptiveFiles, Graph).ObjectFiles); } else if (SpecificFilesToCompile.Count == 0) { Unity.GetAdaptiveFiles(Target, InputFiles.CPPFiles, InputFiles.HeaderFiles, CompileEnvironment, WorkingSet, Rules.ShortName ?? Name, IntermediateDirectory, Graph, out List NormalFiles, out List AdaptiveFiles); LinkInputFiles.AddRange(CompileFilesWithToolChain(Target, ToolChain, CompileEnvironment, ModuleCompileEnvironment, NormalFiles, AdaptiveFiles, Graph).ObjectFiles); } else { LinkInputFiles.AddRange(CompileFilesWithToolChain(Target, ToolChain, CompileEnvironment, ModuleCompileEnvironment, InputFiles.CPPFiles, new List(), Graph).ObjectFiles); } // Compile all the generated CPP files if (GeneratedCppDirectories != null && !CompileEnvironment.bHackHeaderGenerator && SpecificFilesToCompile.Count == 0) { List GeneratedFiles = new List(); foreach (string GeneratedDir in GeneratedCppDirectories) { if (Directory.Exists(GeneratedDir)) { GeneratedFiles.AddRange(Directory.GetFiles(GeneratedDir, "*.gen.cpp")); } } if(GeneratedFiles.Count > 0) { // Create a compile environment for the generated files. We can disable creating debug info here to improve link times. CppCompileEnvironment GeneratedCPPCompileEnvironment = CompileEnvironment; if(GeneratedCPPCompileEnvironment.bCreateDebugInfo && Target.bDisableDebugInfoForGeneratedCode) { GeneratedCPPCompileEnvironment = new CppCompileEnvironment(GeneratedCPPCompileEnvironment); GeneratedCPPCompileEnvironment.bCreateDebugInfo = false; } // Always force include the PCH, even if PCHs are disabled, for generated code. Legacy code can rely on PCHs being included to compile correctly, and this used to be done by UHT manually including it. if(GeneratedCPPCompileEnvironment.PrecompiledHeaderFile == null && Rules.PrivatePCHHeaderFile != null && Rules.PCHUsage != ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs) { FileItem PrivatePchFileItem = FileItem.GetItemByFileReference(FileReference.Combine(ModuleDirectory, Rules.PrivatePCHHeaderFile)); if(!PrivatePchFileItem.Exists) { throw new BuildException("Unable to find private PCH file '{0}', referenced by '{1}'", PrivatePchFileItem.Location, RulesFile); } GeneratedCPPCompileEnvironment = new CppCompileEnvironment(GeneratedCPPCompileEnvironment); GeneratedCPPCompileEnvironment.ForceIncludeFiles.Add(PrivatePchFileItem); } // Compile all the generated files List GeneratedFileItems = new List(); foreach (string GeneratedFilename in GeneratedFiles) { FileItem GeneratedCppFileItem = FileItem.GetItemByPath(GeneratedFilename); if (SpecificFilesToCompile.Count == 0 || SpecificFilesToCompile.Contains(GeneratedCppFileItem.Location)) { GeneratedFileItems.Add(GeneratedCppFileItem); } } if (bModuleUsesUnityBuild) { Unity.GenerateUnityCPPs(Target, GeneratedFileItems, new List(), CompileEnvironment, WorkingSet, (Rules.ShortName ?? Name) + ".gen", IntermediateDirectory, Graph, SourceFileToUnityFile, out List NormalGeneratedFiles, out List AdaptiveGeneratedFiles); LinkInputFiles.AddRange(CompileFilesWithToolChain(Target, ToolChain, GeneratedCPPCompileEnvironment, ModuleCompileEnvironment, NormalGeneratedFiles, AdaptiveGeneratedFiles, Graph).ObjectFiles); } else { LinkInputFiles.AddRange(ToolChain.CompileCPPFiles(GeneratedCPPCompileEnvironment, GeneratedFileItems, IntermediateDirectory, Name, Graph).ObjectFiles); } } } // Compile ISPC files directly if (InputFiles.ISPCFiles.Count > 0) { LinkInputFiles.AddRange(ToolChain.CompileISPCFiles(CompileEnvironment, InputFiles.ISPCFiles, IntermediateDirectory, Graph).ObjectFiles); } // Compile C files directly. Do not use a PCH here, because a C++ PCH is not compatible with C source files. if(InputFiles.CFiles.Count > 0) { LinkInputFiles.AddRange(ToolChain.CompileCPPFiles(ModuleCompileEnvironment, InputFiles.CFiles, IntermediateDirectory, Name, Graph).ObjectFiles); } // Compile CC files directly. if(InputFiles.CCFiles.Count > 0) { LinkInputFiles.AddRange(ToolChain.CompileCPPFiles(CompileEnvironment, InputFiles.CCFiles, IntermediateDirectory, Name, Graph).ObjectFiles); } // Compile MM files directly. if(InputFiles.MMFiles.Count > 0) { LinkInputFiles.AddRange(ToolChain.CompileCPPFiles(CompileEnvironment, InputFiles.MMFiles, IntermediateDirectory, Name, Graph).ObjectFiles); } // Compile RC files. The resource compiler does not work with response files, and using the regular compile environment can easily result in the // command line length exceeding the OS limit. Use the binary compile environment to keep the size down, and require that all include paths // must be specified relative to the resource file itself or Engine/Source. if(InputFiles.RCFiles.Count > 0) { CppCompileEnvironment ResourceCompileEnvironment = new CppCompileEnvironment(BinaryCompileEnvironment); if(Binary != null) { // @todo: This should be in some Windows code somewhere... ResourceCompileEnvironment.Definitions.Add("ORIGINAL_FILE_NAME=\"" + Binary.OutputFilePaths[0].GetFileName() + "\""); } LinkInputFiles.AddRange(ToolChain.CompileRCFiles(ResourceCompileEnvironment, InputFiles.RCFiles, IntermediateDirectory, Graph).ObjectFiles); } // Write the compiled manifest if(Rules.bPrecompile && Target.LinkType == TargetLinkType.Monolithic) { DirectoryReference.CreateDirectory(PrecompiledManifestLocation.Directory); PrecompiledManifest Manifest = new PrecompiledManifest(); Manifest.OutputFiles.AddRange(LinkInputFiles.Select(x => x.Location)); Manifest.WriteIfModified(PrecompiledManifestLocation); } // Write a mapping of unity object file to standalone object file for live coding if(Rules.Target.bWithLiveCoding) { FileReference UnityManifestFile = FileReference.Combine(IntermediateDirectory, "LiveCodingInfo.json"); using (JsonWriter Writer = new JsonWriter(UnityManifestFile)) { Writer.WriteObjectStart(); Writer.WriteObjectStart("RemapUnityFiles"); foreach (IGrouping> UnityGroup in SourceFileToUnityFile.GroupBy(x => x.Value)) { Writer.WriteArrayStart(UnityGroup.Key.Location.GetFileName() + ".obj"); foreach (FileItem SourceFile in UnityGroup.Select(x => x.Key)) { Writer.WriteValue(SourceFile.Location.GetFileName() + ".obj"); } Writer.WriteArrayEnd(); } Writer.WriteObjectEnd(); Writer.WriteObjectEnd(); } } return LinkInputFiles; } /// /// Create a shared PCH template for this module, which allows constructing shared PCH instances in the future /// /// The target which owns this module /// Base compile environment for this target /// Template for shared PCHs public PrecompiledHeaderTemplate CreateSharedPCHTemplate(UEBuildTarget Target, CppCompileEnvironment BaseCompileEnvironment) { CppCompileEnvironment CompileEnvironment = CreateSharedPCHCompileEnvironment(Target, BaseCompileEnvironment); FileItem HeaderFile = FileItem.GetItemByFileReference(FileReference.Combine(ModuleDirectory, Rules.SharedPCHHeaderFile!)); DirectoryReference PrecompiledHeaderDir; if(Rules.bUsePrecompiled) { PrecompiledHeaderDir = DirectoryReference.Combine(Target.ProjectIntermediateDirectory, Name); } else { PrecompiledHeaderDir = IntermediateDirectory; } return new PrecompiledHeaderTemplate(this, CompileEnvironment, HeaderFile, PrecompiledHeaderDir); } /// /// Creates a precompiled header action to generate a new pch file /// /// The toolchain to generate the PCH /// /// /// List of actions to be executed. Additional actions will be added to this list. /// The created PCH instance. private PrecompiledHeaderInstance CreatePrivatePCH(UEToolChain? ToolChain, FileItem HeaderFile, CppCompileEnvironment ModuleCompileEnvironment, IActionGraphBuilder Graph) { // Create the wrapper file, which sets all the definitions needed to compile it FileReference WrapperLocation = FileReference.Combine(IntermediateDirectory, String.Format("PCH.{0}.h", Name)); FileItem WrapperFile = CreatePCHWrapperFile(WrapperLocation, ModuleCompileEnvironment.Definitions, HeaderFile, Graph); // Create a new C++ environment that is used to create the PCH. CppCompileEnvironment CompileEnvironment = new CppCompileEnvironment(ModuleCompileEnvironment); CompileEnvironment.Definitions.Clear(); CompileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Create; CompileEnvironment.PrecompiledHeaderIncludeFilename = WrapperFile.Location; CompileEnvironment.bOptimizeCode = ModuleCompileEnvironment.bOptimizeCode; // Create the action to compile the PCH file. CPPOutput Output; if (ToolChain == null) { Output = new CPPOutput(); } else { Output = ToolChain.CompileCPPFiles(CompileEnvironment, new List() { WrapperFile }, IntermediateDirectory, Name, Graph); } return new PrecompiledHeaderInstance(WrapperFile, CompileEnvironment, Output); } /// /// Generates a precompiled header instance from the given template, or returns an existing one if it already exists /// /// The toolchain being used to build this module /// The PCH template /// Compile environment for the current module /// List of actions to be executed. Additional actions will be added to this list. /// Instance of a PCH public PrecompiledHeaderInstance FindOrCreateSharedPCH(UEToolChain? ToolChain, PrecompiledHeaderTemplate Template, CppCompileEnvironment ModuleCompileEnvironment, IActionGraphBuilder Graph) { PrecompiledHeaderInstance? Instance = Template.Instances.Find(x => IsCompatibleForSharedPCH(x.CompileEnvironment, ModuleCompileEnvironment)); if(Instance == null) { // Create a suffix to distinguish this shared PCH variant from any others. Currently only optimized and non-optimized shared PCHs are supported. string Variant = GetSuffixForSharedPCH(ModuleCompileEnvironment, Template.BaseCompileEnvironment); // Create the wrapper file, which sets all the definitions needed to compile it FileReference WrapperLocation = FileReference.Combine(Template.OutputDir, String.Format("SharedPCH.{0}{1}.h", Template.Module.Name, Variant)); FileItem WrapperFile = CreatePCHWrapperFile(WrapperLocation, Template.BaseCompileEnvironment.Definitions, Template.HeaderFile, Graph); // Create the compile environment for this PCH CppCompileEnvironment CompileEnvironment = new CppCompileEnvironment(Template.BaseCompileEnvironment); CompileEnvironment.Definitions.Clear(); CompileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Create; CompileEnvironment.PrecompiledHeaderIncludeFilename = WrapperFile.Location; CopySettingsForSharedPCH(ModuleCompileEnvironment, CompileEnvironment); // Create the PCH CPPOutput Output; if (ToolChain == null) { Output = new CPPOutput(); } else { Output = ToolChain.CompileCPPFiles(CompileEnvironment, new List() { WrapperFile }, Template.OutputDir, "Shared", Graph); } Instance = new PrecompiledHeaderInstance(WrapperFile, CompileEnvironment, Output); Template.Instances.Add(Instance); } return Instance; } /// /// Determines if a module compile environment is compatible with the given shared PCH compile environment /// /// The module compile environment /// The shared PCH compile environment /// True if the two compile enviroments are compatible private bool IsCompatibleForSharedPCH(CppCompileEnvironment ModuleCompileEnvironment, CppCompileEnvironment CompileEnvironment) { if(ModuleCompileEnvironment.bOptimizeCode != CompileEnvironment.bOptimizeCode) { return false; } if(ModuleCompileEnvironment.bUseRTTI != CompileEnvironment.bUseRTTI) { return false; } if(ModuleCompileEnvironment.bEnableExceptions != CompileEnvironment.bEnableExceptions) { return false; } if(ModuleCompileEnvironment.ShadowVariableWarningLevel != CompileEnvironment.ShadowVariableWarningLevel) { return false; } if(ModuleCompileEnvironment.UnsafeTypeCastWarningLevel != CompileEnvironment.UnsafeTypeCastWarningLevel) { return false; } if(ModuleCompileEnvironment.bEnableUndefinedIdentifierWarnings != CompileEnvironment.bEnableUndefinedIdentifierWarnings) { return false; } if (ModuleCompileEnvironment.CppStandard != CompileEnvironment.CppStandard) { return false; } return true; } /// /// Gets the unique suffix for a shared PCH /// /// The shared PCH compile environment /// The base compile environment /// The unique suffix for the shared PCH private string GetSuffixForSharedPCH(CppCompileEnvironment CompileEnvironment, CppCompileEnvironment BaseCompileEnvironment) { string Variant = ""; if(CompileEnvironment.bOptimizeCode != BaseCompileEnvironment.bOptimizeCode) { if(CompileEnvironment.bOptimizeCode) { Variant += ".Optimized"; } else { Variant += ".NonOptimized"; } } if(CompileEnvironment.bUseRTTI != BaseCompileEnvironment.bUseRTTI) { if (CompileEnvironment.bUseRTTI) { Variant += ".RTTI"; } else { Variant += ".NonRTTI"; } } if (CompileEnvironment.bEnableExceptions != BaseCompileEnvironment.bEnableExceptions) { if (CompileEnvironment.bEnableExceptions) { Variant += ".Exceptions"; } else { Variant += ".NoExceptions"; } } if (CompileEnvironment.ShadowVariableWarningLevel != BaseCompileEnvironment.ShadowVariableWarningLevel) { if (CompileEnvironment.ShadowVariableWarningLevel == WarningLevel.Error) { Variant += ".ShadowErrors"; } else if (CompileEnvironment.ShadowVariableWarningLevel == WarningLevel.Warning) { Variant += ".ShadowWarnings"; } else { Variant += ".NoShadow"; } } if (CompileEnvironment.UnsafeTypeCastWarningLevel != BaseCompileEnvironment.UnsafeTypeCastWarningLevel) { if (CompileEnvironment.UnsafeTypeCastWarningLevel == WarningLevel.Error) { Variant += ".TypeCastErrors"; } else if (CompileEnvironment.UnsafeTypeCastWarningLevel == WarningLevel.Warning) { Variant += ".TypeCastWarnings"; } else { Variant += ".NoTypeCast"; } } if (CompileEnvironment.bEnableUndefinedIdentifierWarnings != BaseCompileEnvironment.bEnableUndefinedIdentifierWarnings) { if (CompileEnvironment.bEnableUndefinedIdentifierWarnings) { Variant += ".Undef"; } else { Variant += ".NoUndef"; } } if (CompileEnvironment.CppStandard != BaseCompileEnvironment.CppStandard) { Variant += String.Format(".{0}", CompileEnvironment.CppStandard); } return Variant; } /// /// Copy settings from the module's compile environment into the environment for the shared PCH /// /// The module compile environment /// The shared PCH compile environment private void CopySettingsForSharedPCH(CppCompileEnvironment ModuleCompileEnvironment, CppCompileEnvironment CompileEnvironment) { CompileEnvironment.bOptimizeCode = ModuleCompileEnvironment.bOptimizeCode; CompileEnvironment.bUseRTTI = ModuleCompileEnvironment.bUseRTTI; CompileEnvironment.bEnableExceptions = ModuleCompileEnvironment.bEnableExceptions; CompileEnvironment.ShadowVariableWarningLevel = ModuleCompileEnvironment.ShadowVariableWarningLevel; CompileEnvironment.UnsafeTypeCastWarningLevel = ModuleCompileEnvironment.UnsafeTypeCastWarningLevel; CompileEnvironment.bEnableUndefinedIdentifierWarnings = ModuleCompileEnvironment.bEnableUndefinedIdentifierWarnings; CompileEnvironment.CppStandard = ModuleCompileEnvironment.CppStandard; } /// /// Compiles the provided CPP unity files. Will /// private CPPOutput CompileFilesWithToolChain( ReadOnlyTargetRules Target, UEToolChain ToolChain, CppCompileEnvironment CompileEnvironment, CppCompileEnvironment ModuleCompileEnvironment, List NormalFiles, List AdaptiveFiles, IActionGraphBuilder Graph) { bool bAdaptiveUnityDisablesPCH = false; if(Rules.PCHUsage == ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs) { if(Rules.bTreatAsEngineModule || Rules.PrivatePCHHeaderFile == null) { bAdaptiveUnityDisablesPCH = Target.bAdaptiveUnityDisablesPCH; } else { bAdaptiveUnityDisablesPCH = Target.bAdaptiveUnityDisablesPCHForProject; } } if (AdaptiveFiles.Count > 0) { if (Target.bAdaptiveUnityCreatesDedicatedPCH) { Graph.AddDiagnostic("[Adaptive Build] Creating dedicated PCH for each excluded file. Set bAdaptiveUnityCreatesDedicatedPCH to false in BuildConfiguration.xml to change this behavior."); } else if (Target.bAdaptiveUnityDisablesPCH) { Graph.AddDiagnostic("[Adaptive Build] Disabling PCH for excluded files. Set bAdaptiveUnityDisablesPCH to false in BuildConfiguration.xml to change this behavior."); } if (Target.bAdaptiveUnityDisablesOptimizations) { Graph.AddDiagnostic("[Adaptive Build] Disabling optimizations for excluded files. Set bAdaptiveUnityDisablesOptimizations to false in BuildConfiguration.xml to change this behavior."); } if (Target.bAdaptiveUnityEnablesEditAndContinue) { Graph.AddDiagnostic("[Adaptive Build] Enabling Edit & Continue for excluded files. Set bAdaptiveUnityEnablesEditAndContinue to false in BuildConfiguration.xml to change this behavior."); } Graph.AddDiagnostic($"[Adaptive Build] Excluded from {Name} unity file: " + String.Join(", ", AdaptiveFiles.Select(File => Path.GetFileName(File.AbsolutePath)))); } if ((!Target.bAdaptiveUnityDisablesOptimizations && !bAdaptiveUnityDisablesPCH && !Target.bAdaptiveUnityCreatesDedicatedPCH) || Target.bStressTestUnity) { NormalFiles = NormalFiles.Concat(AdaptiveFiles).ToList(); AdaptiveFiles = new List(); } CPPOutput OutputFiles = new CPPOutput(); if (NormalFiles.Count > 0) { OutputFiles = ToolChain.CompileCPPFiles(CompileEnvironment, NormalFiles, IntermediateDirectory, Name, Graph); } if (AdaptiveFiles.Count > 0) { // Create the new compile environment. Always turn off PCH due to different compiler settings. CppCompileEnvironment AdaptiveUnityEnvironment = new CppCompileEnvironment(ModuleCompileEnvironment); if(Target.bAdaptiveUnityDisablesOptimizations) { AdaptiveUnityEnvironment.bOptimizeCode = false; } if (Target.bAdaptiveUnityEnablesEditAndContinue) { AdaptiveUnityEnvironment.bSupportEditAndContinue = true; } // Create a per-file PCH CPPOutput AdaptiveOutput; if(Target.bAdaptiveUnityCreatesDedicatedPCH) { AdaptiveOutput = CompileAdaptiveNonUnityFilesWithDedicatedPCH(ToolChain, AdaptiveUnityEnvironment, AdaptiveFiles, IntermediateDirectory, Name, Graph); } else if(bAdaptiveUnityDisablesPCH) { AdaptiveOutput = CompileAdaptiveNonUnityFilesWithoutPCH(ToolChain, AdaptiveUnityEnvironment, AdaptiveFiles, IntermediateDirectory, Name, Graph); } else if(AdaptiveUnityEnvironment.bOptimizeCode != CompileEnvironment.bOptimizeCode || AdaptiveUnityEnvironment.bSupportEditAndContinue != CompileEnvironment.bSupportEditAndContinue) { AdaptiveOutput = CompileAdaptiveNonUnityFiles(ToolChain, AdaptiveUnityEnvironment, AdaptiveFiles, IntermediateDirectory, Name, Graph); } else { AdaptiveOutput = CompileAdaptiveNonUnityFiles(ToolChain, CompileEnvironment, AdaptiveFiles, IntermediateDirectory, Name, Graph); } // Merge output OutputFiles.ObjectFiles.AddRange(AdaptiveOutput.ObjectFiles); } return OutputFiles; } static CPPOutput CompileAdaptiveNonUnityFiles(UEToolChain ToolChain, CppCompileEnvironment CompileEnvironment, List Files, DirectoryReference IntermediateDirectory, string ModuleName, IActionGraphBuilder Graph) { // Write all the definitions out to a separate file CreateHeaderForDefinitions(CompileEnvironment, IntermediateDirectory, "Adaptive", Graph); // Compile the files return ToolChain.CompileCPPFiles(CompileEnvironment, Files, IntermediateDirectory, ModuleName, Graph); } static CPPOutput CompileAdaptiveNonUnityFilesWithoutPCH(UEToolChain ToolChain, CppCompileEnvironment CompileEnvironment, List Files, DirectoryReference IntermediateDirectory, string ModuleName, IActionGraphBuilder Graph) { // Disable precompiled headers CompileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.None; // Write all the definitions out to a separate file CreateHeaderForDefinitions(CompileEnvironment, IntermediateDirectory, "Adaptive", Graph); // Compile the files return ToolChain.CompileCPPFiles(CompileEnvironment, Files, IntermediateDirectory, ModuleName, Graph); } static CPPOutput CompileAdaptiveNonUnityFilesWithDedicatedPCH(UEToolChain ToolChain, CppCompileEnvironment CompileEnvironment, List Files, DirectoryReference IntermediateDirectory, string ModuleName, IActionGraphBuilder Graph) { CPPOutput Output = new CPPOutput(); foreach(FileItem File in Files) { // Build the contents of the wrapper file StringBuilder WrapperContents = new StringBuilder(); using (StringWriter Writer = new StringWriter(WrapperContents)) { string FileString = File.AbsolutePath; if (File.Location.IsUnderDirectory(Unreal.RootDirectory)) { FileString = File.Location.MakeRelativeTo(UnrealBuildTool.EngineSourceDirectory); } FileString = FileString.Replace('\\', '/'); Writer.WriteLine("// Dedicated PCH for {0}", FileString); Writer.WriteLine(); WriteDefinitions(CompileEnvironment.Definitions, Writer); Writer.WriteLine(); using(StreamReader Reader = new StreamReader(File.Location.FullName)) { CppIncludeParser.CopyIncludeDirectives(Reader, Writer); } } // Write the PCH header FileReference DedicatedPchLocation = FileReference.Combine(IntermediateDirectory, String.Format("PCH.Dedicated.{0}.h", File.Location.GetFileNameWithoutExtension())); FileItem DedicatedPchFile = Graph.CreateIntermediateTextFile(DedicatedPchLocation, WrapperContents.ToString(), StringComparison.Ordinal); // Create a new C++ environment to compile the PCH CppCompileEnvironment PchEnvironment = new CppCompileEnvironment(CompileEnvironment); PchEnvironment.Definitions.Clear(); PchEnvironment.UserIncludePaths.Add(File.Location.Directory); // Need to be able to include headers in the same directory as the source file PchEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Create; PchEnvironment.PrecompiledHeaderIncludeFilename = DedicatedPchFile.Location; // Create the action to compile the PCH file. CPPOutput PchOutput = ToolChain.CompileCPPFiles(PchEnvironment, new List() { DedicatedPchFile }, IntermediateDirectory, ModuleName, Graph); Output.ObjectFiles.AddRange(PchOutput.ObjectFiles); // Create a new C++ environment to compile the original file CppCompileEnvironment FileEnvironment = new CppCompileEnvironment(CompileEnvironment); FileEnvironment.Definitions.Clear(); FileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Include; FileEnvironment.PrecompiledHeaderIncludeFilename = DedicatedPchFile.Location; FileEnvironment.PrecompiledHeaderFile = PchOutput.PrecompiledHeaderFile; // Create the action to compile the PCH file. CPPOutput FileOutput = ToolChain.CompileCPPFiles(FileEnvironment, new List() { File }, IntermediateDirectory, ModuleName, Graph); Output.ObjectFiles.AddRange(FileOutput.ObjectFiles); } return Output; } /// /// Configure precompiled headers for this module /// /// The target being built /// The toolchain to build with /// The current compile environment /// List of files that will be linked for the target /// List of build actions CppCompileEnvironment SetupPrecompiledHeaders(ReadOnlyTargetRules Target, UEToolChain? ToolChain, CppCompileEnvironment CompileEnvironment, List LinkInputFiles, IActionGraphBuilder Graph) { if (Target.bUsePCHFiles && Rules.PCHUsage != ModuleRules.PCHUsageMode.NoPCHs) { // If this module doesn't need a shared PCH, configure that if(Rules.PrivatePCHHeaderFile != null && (Rules.PCHUsage == ModuleRules.PCHUsageMode.NoSharedPCHs || Rules.PCHUsage == ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs)) { PrecompiledHeaderInstance Instance = CreatePrivatePCH(ToolChain, FileItem.GetItemByFileReference(FileReference.Combine(ModuleDirectory, Rules.PrivatePCHHeaderFile)), CompileEnvironment, Graph); CompileEnvironment = new CppCompileEnvironment(CompileEnvironment); CompileEnvironment.Definitions.Clear(); CompileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Include; CompileEnvironment.PrecompiledHeaderIncludeFilename = Instance.HeaderFile.Location; CompileEnvironment.PrecompiledHeaderFile = Instance.Output.PrecompiledHeaderFile; LinkInputFiles.AddRange(Instance.Output.ObjectFiles); } // Try to find a suitable shared PCH for this module if (CompileEnvironment.PrecompiledHeaderIncludeFilename == null && CompileEnvironment.SharedPCHs.Count > 0 && !CompileEnvironment.bIsBuildingLibrary && Rules.PCHUsage != ModuleRules.PCHUsageMode.NoSharedPCHs) { // Find all the dependencies of this module HashSet ReferencedModules = new HashSet(); GetAllDependencyModules(new List(), ReferencedModules, bIncludeDynamicallyLoaded: false, bForceCircular: false, bOnlyDirectDependencies: true); // Find the first shared PCH module we can use PrecompiledHeaderTemplate Template = CompileEnvironment.SharedPCHs.FirstOrDefault(x => ReferencedModules.Contains(x.Module)); if(Template != null && Template.IsValidFor(CompileEnvironment)) { PrecompiledHeaderInstance Instance = FindOrCreateSharedPCH(ToolChain, Template, CompileEnvironment, Graph); FileReference PrivateDefinitionsFile = FileReference.Combine(IntermediateDirectory, String.Format("Definitions.{0}.h", Name)); FileItem PrivateDefinitionsFileItem; using (StringWriter Writer = new StringWriter()) { // Remove the module _API definition for cases where there are circular dependencies between the shared PCH module and modules using it Writer.WriteLine("#undef {0}", ModuleApiDefine); // Games may choose to use shared PCHs from the engine, so allow them to change the value of these macros if(!Rules.bTreatAsEngineModule) { Writer.WriteLine("#undef UE_IS_ENGINE_MODULE"); Writer.WriteLine("#undef DEPRECATED_FORGAME"); Writer.WriteLine("#define DEPRECATED_FORGAME DEPRECATED"); Writer.WriteLine("#undef UE_DEPRECATED_FORGAME"); Writer.WriteLine("#define UE_DEPRECATED_FORGAME UE_DEPRECATED"); } WriteDefinitions(CompileEnvironment.Definitions, Writer); PrivateDefinitionsFileItem = Graph.CreateIntermediateTextFile(PrivateDefinitionsFile, Writer.ToString(), StringComparison.Ordinal); } CompileEnvironment = new CppCompileEnvironment(CompileEnvironment); CompileEnvironment.Definitions.Clear(); CompileEnvironment.ForceIncludeFiles.Add(PrivateDefinitionsFileItem); CompileEnvironment.PrecompiledHeaderAction = PrecompiledHeaderAction.Include; CompileEnvironment.PrecompiledHeaderIncludeFilename = Instance.HeaderFile.Location; CompileEnvironment.PrecompiledHeaderFile = Instance.Output.PrecompiledHeaderFile; LinkInputFiles.AddRange(Instance.Output.ObjectFiles); } } } return CompileEnvironment; } /// /// Creates a header file containing all the preprocessor definitions for a compile environment, and force-include it. We allow a more flexible syntax for preprocessor definitions than /// is typically allowed on the command line (allowing function macros or double-quote characters, for example). Ensuring all definitions are specified in a header files ensures consistent /// behavior. /// /// The compile environment /// Directory to create the intermediate file /// Suffix for the included file /// The action graph being built static void CreateHeaderForDefinitions(CppCompileEnvironment CompileEnvironment, DirectoryReference IntermediateDirectory, string? HeaderSuffix, IActionGraphBuilder Graph) { if(CompileEnvironment.Definitions.Count > 0) { string PrivateDefinitionsName = "Definitions.h"; if(!String.IsNullOrEmpty(HeaderSuffix)) { PrivateDefinitionsName = $"Definitions.{HeaderSuffix}.h"; } FileReference PrivateDefinitionsFile = FileReference.Combine(IntermediateDirectory, PrivateDefinitionsName); using (StringWriter Writer = new StringWriter()) { WriteDefinitions(CompileEnvironment.Definitions, Writer); CompileEnvironment.Definitions.Clear(); FileItem PrivateDefinitionsFileItem = Graph.CreateIntermediateTextFile(PrivateDefinitionsFile, Writer.ToString(), StringComparison.Ordinal); CompileEnvironment.ForceIncludeFiles.Add(PrivateDefinitionsFileItem); } } } /// /// Creates header files from ISPC for inclusion and adds them as dependencies. /// /// The toolchain to generate the PCH /// Compile environment /// List of ISPC source files /// Directory to create the intermediate file /// List of actions to be executed. Additional actions will be added to this list. static void CreateHeadersForISPC(UEToolChain ToolChain, CppCompileEnvironment CompileEnvironment, List InputFiles, DirectoryReference IntermediateDirectory, IActionGraphBuilder Graph) { CPPOutput Output = ToolChain.GenerateISPCHeaders(CompileEnvironment, InputFiles, IntermediateDirectory, Graph); CompileEnvironment.AdditionalPrerequisites.AddRange(Output.GeneratedHeaderFiles); CompileEnvironment.UserIncludePaths.Add(IntermediateDirectory); } /// /// Create a header file containing the module definitions, which also includes the PCH itself. Including through another file is necessary on /// Clang, since we get warnings about #pragma once otherwise, but it also allows us to consistently define the preprocessor state on all /// platforms. /// /// The output file to create /// Definitions required by the PCH /// The PCH file to include /// The action graph builder /// FileItem for the created file static FileItem CreatePCHWrapperFile(FileReference OutputFile, IEnumerable Definitions, FileItem IncludedFile, IActionGraphBuilder Graph) { // Build the contents of the wrapper file StringBuilder WrapperContents = new StringBuilder(); using (StringWriter Writer = new StringWriter(WrapperContents)) { string IncludeFileString = IncludedFile.AbsolutePath; if (IncludedFile.Location.IsUnderDirectory(Unreal.RootDirectory)) { IncludeFileString = IncludedFile.Location.MakeRelativeTo(UnrealBuildTool.EngineSourceDirectory); } IncludeFileString = IncludeFileString.Replace('\\', '/'); Writer.WriteLine("// PCH for {0}", IncludeFileString); WriteDefinitions(Definitions, Writer); Writer.WriteLine("#include \"{0}\"", IncludeFileString); } // Create the item FileItem WrapperFile = Graph.CreateIntermediateTextFile(OutputFile, WrapperContents.ToString(), StringComparison.Ordinal); // Touch it if the included file is newer, to make sure our timestamp dependency checking is accurate. if (IncludedFile.LastWriteTimeUtc > WrapperFile.LastWriteTimeUtc) { File.SetLastWriteTimeUtc(WrapperFile.AbsolutePath, DateTime.UtcNow); WrapperFile.ResetCachedInfo(); } return WrapperFile; } /// /// Write a list of macro definitions to an output file /// /// List of definitions /// Writer to receive output static void WriteDefinitions(IEnumerable Definitions, TextWriter Writer) { Writer.WriteLine("// Generated by UnrealBuildTool (UEBuildModuleCPP.cs)"); foreach(string Definition in Definitions) { int EqualsIdx = Definition.IndexOf('='); if(EqualsIdx == -1) { Writer.WriteLine("#define {0} 1", Definition); } else { Writer.WriteLine("#define {0} {1}", Definition.Substring(0, EqualsIdx), Definition.Substring(EqualsIdx + 1)); } } } /// /// Checks that the first header included by the source files in this module all include the same header /// /// The target being compiled /// Compile environment for the module /// All header files for this module /// List of C++ source files private void CheckFirstIncludeMatchesEachCppFile(ReadOnlyTargetRules Target, CppCompileEnvironment ModuleCompileEnvironment, List HeaderFiles, List CppFiles) { if(Rules.PCHUsage == ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs) { if(InvalidIncludeDirectiveMessages == null) { // Find headers used by the source file. Dictionary NameToHeaderFile = new Dictionary(); foreach(FileItem HeaderFile in HeaderFiles) { NameToHeaderFile[HeaderFile.Location.GetFileNameWithoutExtension()] = HeaderFile.Location; } // Find the directly included files for each source file, and make sure it includes the matching header if possible InvalidIncludeDirectiveMessages = new List(); if (Rules != null && Rules.bEnforceIWYU && Target.bEnforceIWYU) { foreach (FileItem CppFile in CppFiles) { string? FirstInclude = ModuleCompileEnvironment.MetadataCache.GetFirstInclude(CppFile); if(FirstInclude != null) { string IncludeName = Path.GetFileNameWithoutExtension(FirstInclude); string ExpectedName = CppFile.Location.GetFileNameWithoutExtension(); if (String.Compare(IncludeName, ExpectedName, StringComparison.OrdinalIgnoreCase) != 0) { FileReference? HeaderFile; if (NameToHeaderFile.TryGetValue(ExpectedName, out HeaderFile) && !IgnoreMismatchedHeader(ExpectedName)) { InvalidIncludeDirectiveMessages.Add(String.Format("{0}(1): error: Expected {1} to be first header included.", CppFile.Location, HeaderFile.GetFileName())); } } } } } } } } private bool IgnoreMismatchedHeader(string ExpectedName) { switch(ExpectedName) { case "DynamicRHI": case "RHICommandList": case "RHIUtilities": return true; } switch(Name) { case "D3D11RHI": case "D3D12RHI": case "VulkanRHI": case "OpenGLDrv": case "MetalRHI": case "AGXRHI": return true; } return false; } /// /// Determine whether optimization should be enabled for a given target /// /// The optimization setting from the rules file /// The active target configuration /// Whether the current module is an engine module /// True if optimization should be enabled public static bool ShouldEnableOptimization(ModuleRules.CodeOptimization Setting, UnrealTargetConfiguration Configuration, bool bIsEngineModule) { switch(Setting) { case ModuleRules.CodeOptimization.Never: return false; case ModuleRules.CodeOptimization.Default: case ModuleRules.CodeOptimization.InNonDebugBuilds: return (Configuration == UnrealTargetConfiguration.Debug)? false : (Configuration != UnrealTargetConfiguration.DebugGame || bIsEngineModule); case ModuleRules.CodeOptimization.InShippingBuildsOnly: return (Configuration == UnrealTargetConfiguration.Shipping); default: return true; } } public CppCompileEnvironment CreateCompileEnvironmentForIntellisense(ReadOnlyTargetRules Target, CppCompileEnvironment BaseCompileEnvironment) { CppCompileEnvironment CompileEnvironment = CreateModuleCompileEnvironment(Target, BaseCompileEnvironment); CompileEnvironment = SetupPrecompiledHeaders(Target, null, CompileEnvironment, new List(), new NullActionGraphBuilder()); CreateHeaderForDefinitions(CompileEnvironment, IntermediateDirectory, null, new NullActionGraphBuilder()); return CompileEnvironment; } /// /// Creates a compile environment from a base environment based on the module settings. /// /// Rules for the target being built /// An existing environment to base the module compile environment on. /// The new module compile environment. public CppCompileEnvironment CreateModuleCompileEnvironment(ReadOnlyTargetRules Target, CppCompileEnvironment BaseCompileEnvironment) { CppCompileEnvironment Result = new CppCompileEnvironment(BaseCompileEnvironment); // Override compile environment Result.bUseUnity = Rules.bUseUnity; Result.bOptimizeCode = ShouldEnableOptimization(Rules.OptimizeCode, Target.Configuration, Rules.bTreatAsEngineModule); Result.bUseRTTI |= Rules.bUseRTTI; Result.bUseAVX = Rules.bUseAVX; Result.bEnableBufferSecurityChecks = Rules.bEnableBufferSecurityChecks; Result.MinSourceFilesForUnityBuildOverride = Rules.MinSourceFilesForUnityBuildOverride; Result.MinFilesUsingPrecompiledHeaderOverride = Rules.MinFilesUsingPrecompiledHeaderOverride; Result.bBuildLocallyWithSNDBS = Rules.bBuildLocallyWithSNDBS; Result.bEnableExceptions |= Rules.bEnableExceptions; Result.bEnableObjCExceptions |= Rules.bEnableObjCExceptions; Result.ShadowVariableWarningLevel = Rules.ShadowVariableWarningLevel; Result.UnsafeTypeCastWarningLevel = Rules.UnsafeTypeCastWarningLevel; Result.bEnableUndefinedIdentifierWarnings = Rules.bEnableUndefinedIdentifierWarnings; // If the module overrides the C++ language version, override it on the compile environment if (Rules.CppStandard != CppStandardVersion.Default) { Result.CppStandard = Rules.CppStandard; } if (Target.bEnableCppModules && Result.CppStandard == CppStandardVersion.Default) { Result.CppStandard = CppStandardVersion.Cpp20; } // Set the macro used to check whether monolithic headers can be used if (Rules.bTreatAsEngineModule && (!Rules.bEnforceIWYU || !Target.bEnforceIWYU)) { Result.Definitions.Add("SUPPRESS_MONOLITHIC_HEADER_WARNINGS=1"); } // Add a macro for when we're compiling an engine module, to enable additional compiler diagnostics through code. if (Rules.bTreatAsEngineModule) { Result.Definitions.Add("UE_IS_ENGINE_MODULE=1"); } else { Result.Definitions.Add("UE_IS_ENGINE_MODULE=0"); } // For game modules, set the define for the project and target names, which will be used by the IMPLEMENT_PRIMARY_GAME_MODULE macro. if (!Rules.bTreatAsEngineModule) { // Make sure we don't set any define for a non-engine module that's under the engine directory (eg. UnrealGame) if (Target.ProjectFile != null && RulesFile.IsUnderDirectory(Target.ProjectFile.Directory)) { string ProjectName = Target.ProjectFile.GetFileNameWithoutExtension(); Result.Definitions.Add(String.Format("UE_PROJECT_NAME={0}", ProjectName)); Result.Definitions.Add(String.Format("UE_TARGET_NAME={0}", Target.Name)); } } // Add the module's public and private definitions. Result.Definitions.AddRange(PublicDefinitions); Result.Definitions.AddRange(Rules.PrivateDefinitions); // Add the project definitions if(!Rules.bTreatAsEngineModule) { Result.Definitions.AddRange(Rules.Target.ProjectDefinitions); } // Setup the compile environment for the module. SetupPrivateCompileEnvironment(Result.UserIncludePaths, Result.SystemIncludePaths, Result.ModuleInterfacePaths, Result.Definitions, Result.AdditionalFrameworks, Result.AdditionalPrerequisites, Rules.bLegacyPublicIncludePaths); return Result; } /// /// Creates a compile environment for a shared PCH from a base environment based on the module settings. /// /// The target being built /// An existing environment to base the module compile environment on. /// The new shared PCH compile environment. public CppCompileEnvironment CreateSharedPCHCompileEnvironment(UEBuildTarget Target, CppCompileEnvironment BaseCompileEnvironment) { CppCompileEnvironment CompileEnvironment = new CppCompileEnvironment(BaseCompileEnvironment); // Use the default optimization setting for CompileEnvironment.bOptimizeCode = ShouldEnableOptimization(ModuleRules.CodeOptimization.Default, Target.Configuration, Rules.bTreatAsEngineModule); // Override compile environment CompileEnvironment.bIsBuildingDLL = !Target.ShouldCompileMonolithic(); CompileEnvironment.bIsBuildingLibrary = false; // Add a macro for when we're compiling an engine module, to enable additional compiler diagnostics through code. if (Rules.bTreatAsEngineModule) { CompileEnvironment.Definitions.Add("UE_IS_ENGINE_MODULE=1"); } else { CompileEnvironment.Definitions.Add("UE_IS_ENGINE_MODULE=0"); } // Add the module's private definitions. CompileEnvironment.Definitions.AddRange(PublicDefinitions); // Find all the modules that are part of the public compile environment for this module. Dictionary ModuleToIncludePathsOnlyFlag = new Dictionary(); FindModulesInPublicCompileEnvironment(ModuleToIncludePathsOnlyFlag); // Now set up the compile environment for the modules in the original order that we encountered them foreach (UEBuildModule Module in ModuleToIncludePathsOnlyFlag.Keys) { Module.AddModuleToCompileEnvironment(this, null, CompileEnvironment.UserIncludePaths, CompileEnvironment.SystemIncludePaths, CompileEnvironment.ModuleInterfacePaths, CompileEnvironment.Definitions, CompileEnvironment.AdditionalFrameworks, CompileEnvironment.AdditionalPrerequisites, Rules.bLegacyPublicIncludePaths); } return CompileEnvironment; } /// /// Finds all the source files that should be built for this module /// /// The platform the module is being built for /// Map of directory to source files inside it /// Set of source files that should be built public InputFileCollection FindInputFiles(UnrealTargetPlatform Platform, Dictionary DirectoryToSourceFiles) { ReadOnlyHashSet ExcludedNames = UEBuildPlatform.GetBuildPlatform(Platform).GetExcludedFolderNames(); InputFileCollection InputFiles = new InputFileCollection(); SourceDirectories = new HashSet(); foreach (DirectoryReference Dir in ModuleDirectories) { DirectoryItem ModuleDirectoryItem = DirectoryItem.GetItemByDirectoryReference(Dir); FindInputFilesFromDirectoryRecursive(ModuleDirectoryItem, ExcludedNames, SourceDirectories, DirectoryToSourceFiles, InputFiles); } return InputFiles; } /// /// Finds all the source files that should be built for this module /// /// Directory to search from /// Set of excluded directory names (eg. other platforms) /// Set of all non-empty source directories. /// Map from directory to source files inside it /// Collection of source files, categorized by type static void FindInputFilesFromDirectoryRecursive(DirectoryItem BaseDirectory, ReadOnlyHashSet ExcludedNames, HashSet SourceDirectories, Dictionary DirectoryToSourceFiles, InputFileCollection InputFiles) { foreach(DirectoryItem SubDirectory in BaseDirectory.EnumerateDirectories()) { if(!ExcludedNames.Contains(SubDirectory.Name)) { FindInputFilesFromDirectoryRecursive(SubDirectory, ExcludedNames, SourceDirectories, DirectoryToSourceFiles, InputFiles); } } FileItem[] SourceFiles = FindInputFilesFromDirectory(BaseDirectory, InputFiles); if(SourceFiles.Length > 0) { SourceDirectories.Add(BaseDirectory.Location); } DirectoryToSourceFiles.Add(BaseDirectory, SourceFiles); } /// /// Finds the input files that should be built for this module, from a given directory /// /// /// /// Array of source files static FileItem[] FindInputFilesFromDirectory(DirectoryItem BaseDirectory, InputFileCollection InputFiles) { List SourceFiles = new List(); foreach(FileItem InputFile in BaseDirectory.EnumerateFiles()) { if (InputFile.HasExtension(".h")) { InputFiles.HeaderFiles.Add(InputFile); } else if (InputFile.HasExtension(".isph")) { InputFiles.ISPCHeaderFiles.Add(InputFile); } else if (InputFile.HasExtension(".cpp")) { SourceFiles.Add(InputFile); InputFiles.CPPFiles.Add(InputFile); } else if (InputFile.HasExtension(".ixx")) { SourceFiles.Add(InputFile); InputFiles.IXXFiles.Add(InputFile); } else if (InputFile.HasExtension(".c")) { SourceFiles.Add(InputFile); InputFiles.CFiles.Add(InputFile); } else if (InputFile.HasExtension(".cc")) { SourceFiles.Add(InputFile); InputFiles.CCFiles.Add(InputFile); } else if (InputFile.HasExtension(".m") || InputFile.HasExtension(".mm")) { SourceFiles.Add(InputFile); InputFiles.MMFiles.Add(InputFile); } else if (InputFile.HasExtension(".rc")) { SourceFiles.Add(InputFile); InputFiles.RCFiles.Add(InputFile); } else if (InputFile.HasExtension(".ispc")) { SourceFiles.Add(InputFile); InputFiles.ISPCFiles.Add(InputFile); } } return SourceFiles.ToArray(); } /// /// Gets a set of source files for the given directory. Used to detect when the makefile is out of date. /// /// /// Array of source files public static FileItem[] GetSourceFiles(DirectoryItem Directory) { return FindInputFilesFromDirectory(Directory, new InputFileCollection()); } } }