Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/ScriptCompiler.cs

339 lines
12 KiB
C#
Raw Normal View History

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.Win32;
using System.Reflection;
using System.Diagnostics;
using UnrealBuildTool;
using Tools.DotNETCommon.CaselessDictionary;
namespace AutomationTool
{
/// <summary>
/// Exception thrown by PreprocessScriptFile.
/// </summary>
class CompilationException : AutomationException
{
public CompilationException(string Filename, int StartLine, int StartColumn, int EndLine, int EndColumn, string Message, params string[] Args)
: base(String.Format("Compilation Failed.\n>{0}({1},{2},{3},{4}): error: {5}", Path.GetFullPath(Filename), StartLine + 1, StartColumn + 1, EndLine + 1, EndColumn + 1,
String.Format(Message, Args)))
{
}
}
/// <summary>
/// Compiles and loads script assemblies.
/// </summary>
class ScriptCompiler
{
#region Fields
private CaselessDictionary<Type> ScriptCommands;
#if DEBUG
const string BuildConfig = "Debug";
#else
const string BuildConfig = "Development";
#endif
const string DefaultScriptsDLLName = "AutomationScripts.Automation.dll";
#endregion
#region Compilation
public ScriptCompiler()
{
}
/// <summary>
/// Finds and/or compiles all script files and assemblies.
/// </summary>
/// <param name="ScriptsForProjectFileName">Path to the current project. May be null, in which case we compile scripts for all projects.</param>
/// <param name="AdditionalScriptsFolders">Additional script fodlers to look for source files in.</param>
public void FindAndCompileAllScripts(string ScriptsForProjectFileName, List<string> AdditionalScriptsFolders)
{
Summary: running UAT from VS is simpler and faster. UEB-261 - Ensure that compiling AutomationTool in VS will compile all other Automation Projects * Just set AutomationTool as your startup project and pass the command to execute. * VS will build the script modules at build time, instead of every time at runtime. * To make this happen, "UBT.exe -ProjectFiles" now generates a companion AutomationTool.csproj.References that make AutomationTool depend on all Automation modules. * AutomationTool.exe defaults to not building script modules at runtime. Pass -compile if you want to dynamically build them. * Without the .references file, AutomationTool will only build itself and you will need to pass -compile. * RunUAT.bat still works that same, defaulting to runtime compilation and supporting -nocompile flag. It then passes -compile (or nothing) to AutomationTool. Other * All Automation projects target .Net 4.5. Some already were and had hard dependencies on them (Rocket and SyncGithub -> Octokit). Now that AutomationTool directly depends on them, everything had to use .Net 4.5. * Decoupled logic for -NoCompile and -NoCompileEditor. The flags are still confusing, but -NoCompile is no longer linked to -NoCompileEditor. * Had to leave in stub support in UAT for -NoCompile else RunUAT.bat passes it along and UAT complains that it doesn't understand it. * Added a CommandUtils.Run option to support run command, but still output the run duration. * Reduced the verbosity when UAT.proj is run from dozens of lines per module to a single Module -> Output line. It was looking like there were problems, but it was just msbuild spew. #codereview:ben.marsh [CL 2615060 by Wes Hunt in Main branch]
2015-07-09 10:15:37 -04:00
bool DoCompile = false;
if (GlobalCommandLine.Compile)
{
Summary: running UAT from VS is simpler and faster. UEB-261 - Ensure that compiling AutomationTool in VS will compile all other Automation Projects * Just set AutomationTool as your startup project and pass the command to execute. * VS will build the script modules at build time, instead of every time at runtime. * To make this happen, "UBT.exe -ProjectFiles" now generates a companion AutomationTool.csproj.References that make AutomationTool depend on all Automation modules. * AutomationTool.exe defaults to not building script modules at runtime. Pass -compile if you want to dynamically build them. * Without the .references file, AutomationTool will only build itself and you will need to pass -compile. * RunUAT.bat still works that same, defaulting to runtime compilation and supporting -nocompile flag. It then passes -compile (or nothing) to AutomationTool. Other * All Automation projects target .Net 4.5. Some already were and had hard dependencies on them (Rocket and SyncGithub -> Octokit). Now that AutomationTool directly depends on them, everything had to use .Net 4.5. * Decoupled logic for -NoCompile and -NoCompileEditor. The flags are still confusing, but -NoCompile is no longer linked to -NoCompileEditor. * Had to leave in stub support in UAT for -NoCompile else RunUAT.bat passes it along and UAT complains that it doesn't understand it. * Added a CommandUtils.Run option to support run command, but still output the run duration. * Reduced the verbosity when UAT.proj is run from dozens of lines per module to a single Module -> Output line. It was looking like there were problems, but it was just msbuild spew. #codereview:ben.marsh [CL 2615060 by Wes Hunt in Main branch]
2015-07-09 10:15:37 -04:00
DoCompile = true;
}
// Change to Engine\Source (if exists) to properly discover all UBT classes
var OldCWD = Environment.CurrentDirectory;
var UnrealBuildToolCWD = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "Engine", "Source");
if (Directory.Exists(UnrealBuildToolCWD))
{
Environment.CurrentDirectory = UnrealBuildToolCWD;
}
// Register all the classes inside UBT
Log.TraceVerbose("Registering UBT Classes.");
UnrealBuildTool.UnrealBuildTool.RegisterAllUBTClasses();
Environment.CurrentDirectory = OldCWD;
// Compile only if not disallowed.
if (DoCompile && !String.IsNullOrEmpty(CommandUtils.CmdEnv.MsBuildExe))
{
CleanupScriptsAssemblies();
FindAndCompileScriptModules(ScriptsForProjectFileName, AdditionalScriptsFolders);
}
var ScriptAssemblies = new List<Assembly>();
LoadPreCompiledScriptAssemblies(ScriptAssemblies);
// Setup platforms
Platform.InitializePlatforms(ScriptAssemblies.ToArray());
// Instantiate all the automation classes for interrogation
Log.TraceVerbose("Creating commands.");
ScriptCommands = new CaselessDictionary<Type>();
foreach (var CompiledScripts in ScriptAssemblies)
{
foreach (var ClassType in CompiledScripts.GetTypes())
{
if (ClassType.IsSubclassOf(typeof(BuildCommand)) && ClassType.IsAbstract == false)
{
if (ScriptCommands.ContainsKey(ClassType.Name) == false)
{
ScriptCommands.Add(ClassType.Name, ClassType);
}
else
{
Log.TraceWarning("Unable to add command {0} twice. Previous: {1}, Current: {2}", ClassType.Name,
ClassType.AssemblyQualifiedName, ScriptCommands[ClassType.Name].AssemblyQualifiedName);
}
}
}
}
}
private static void FindAndCompileScriptModules(string ScriptsForProjectFileName, List<string> AdditionalScriptsFolders)
{
Copying //UE4/Dev-Build to //UE4/Main ========================== MAJOR FEATURES + CHANGES ========================== Change 2828332 on 2016/01/14 by Matthew.Griffin Ensure that warnings from Dynamic Compilation of build.cs files are propagated up to user when compiling in Visual Studio etc. Change 2828335 on 2016/01/14 by Matthew.Griffin Renaming AddThirdParty... functions to AddEngineThirdParty... so it's clear we're talking about third party libraries within the engine directory. This removes another couple of RunningRocket checks and uses the IsMonolithic property now. Change 2831365 on 2016/01/16 by Ben.Marsh Add a UAT command to remove any temp storage directories older than a specified length of time, and disable the GUBP node to do so when running with -NewEC. Better to set this up as a separate scheduled task for each temp storage folder we have, rather than run it as part of GUBP builds above a certain size. Change 2832166 on 2016/01/18 by Ben.Marsh Make INI file parsing much more tolerant to errors, because it prevents the build system from starting up. Now outputs warnings rather than throwing exceptions. Change 2835725 on 2016/01/20 by Matthew.Griffin Removed more uses of RunningRocket functions Switch Linux staging check to see if required files exist until switched to using receipts Always stage CrashReporter for Linux Mac and Linux will not use Compile Lean And Mean when generating project files Allow normal DynamicCompilation check to occur in binary builds Allow XMPP dependency on WebRTC now that it's publically distributed Change 2835864 on 2016/01/20 by Matthew.Griffin Made sure -rocket is passed when generating project files as this has to work from UnrealVersionSelector with every version of the Engine we've released. Also tidied up some arguments that are set in UVS and never used. Change 2839932 on 2016/01/22 by Matthew.Griffin Removed last uses of RunningRocket from UnrealBuildTool Changed Mac Build.sh so that it doesn't try to build UBT so that it can be used in installed builds the same way as windows. Combined code doing very similar things for single games and rocket projects. Removed check on whether to add client and server targets in Rocket as we don't distribute the .target.cs files. Removed RunningRocket function and the code checking for -rocket on the command line Change 2846971 on 2016/01/28 by Matthew.Griffin Replaced RunningRocket with IsEngineInstalled for all Engine programs that won't need to be built. Change 2853879 on 2016/02/03 by Matthew.Griffin Added UnrealLightmass to list of programs to build when Build Solution is used and Editor configuration is selected #jira UE-25666 #lockdown Nick.Penwarden [CL 2854567 by Ben Marsh in Main branch]
2016-02-03 15:40:40 -05:00
Log.TraceInformation("Compiling scripts.");
var OldCWD = Environment.CurrentDirectory;
var UnrealBuildToolCWD = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "Engine", "Source");
Environment.CurrentDirectory = UnrealBuildToolCWD;
// Configure the rules compiler
// Get all game folders and convert them to build subfolders.
List<DirectoryReference> AllGameFolders;
if(ScriptsForProjectFileName == null)
{
AllGameFolders = UnrealBuildTool.UEBuildTarget.DiscoverAllGameFolders();
}
else
{
AllGameFolders = new List<DirectoryReference>{ new DirectoryReference(Path.GetDirectoryName(ScriptsForProjectFileName)) };
}
var AllAdditionalScriptFolders = new List<DirectoryReference>(AdditionalScriptsFolders.Select(x => new DirectoryReference(x)));
foreach (var Folder in AllGameFolders)
{
var GameBuildFolder = DirectoryReference.Combine(Folder, "Build");
if (GameBuildFolder.Exists())
{
AllAdditionalScriptFolders.Add(GameBuildFolder);
}
}
Log.TraceVerbose("Discovering game folders.");
var DiscoveredModules = UnrealBuildTool.RulesCompiler.FindAllRulesSourceFiles(UnrealBuildTool.RulesCompiler.RulesFileType.AutomationModule, GameFolders: AllGameFolders, ForeignPlugins: null, AdditionalSearchPaths: AllAdditionalScriptFolders);
var ModulesToCompile = new List<string>(DiscoveredModules.Count);
foreach (var ModuleFilename in DiscoveredModules)
{
if (HostPlatform.Current.IsScriptModuleSupported(ModuleFilename.GetFileNameWithoutAnyExtensions()))
{
ModulesToCompile.Add(ModuleFilename.FullName);
}
else
{
CommandUtils.LogVerbose("Script module {0} filtered by the Host Platform and will not be compiled.", ModuleFilename);
}
}
if ((UnrealBuildTool.BuildHostPlatform.Current.Platform == UnrealBuildTool.UnrealTargetPlatform.Win64) ||
(UnrealBuildTool.BuildHostPlatform.Current.Platform == UnrealBuildTool.UnrealTargetPlatform.Win32))
{
string Modules = string.Join(";", ModulesToCompile.ToArray());
var UATProj = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, @"Engine\Source\Programs\AutomationTool\Scripts\UAT.proj");
var CmdLine = String.Format("\"{0}\" /p:Modules=\"{1}\" /p:Configuration={2} /verbosity:minimal /nologo", UATProj, Modules, BuildConfig);
Copying //UE4/Dev-Framework to Dev-Main (//UE4/Dev-Main) @ 2855699 #lockdown Nick.Penwarden ========================== MAJOR FEATURES + CHANGES ========================== Change 2839897 on 2016/01/22 by Ori.Cohen Allow static mesh editor to specify a default collision profile. #rb Lina.Halper #UE-2836 Change 2840489 on 2016/01/22 by Ori.Cohen Fix collision customization so that it respects const editing property #rb Marc.Audy Change 2840528 on 2016/01/22 by Ori.Cohen Fix compile error and actually get value from attribute Change 2840672 on 2016/01/22 by Zak.Middleton #ue4 - Include data from USkinnedMeshComponent in USkeletalMeshComponent::GetResourceSize(). #rb Michael.Noland Change 2841314 on 2016/01/24 by Marc.Audy Fix depressingly frequent misspellings of 'suppress' Change 2841323 on 2016/01/24 by Marc.Audy Reserve worst case memory for TSet Intersect, Union, and Difference to avoid memory allocations during iteration Ensure that TSet Intersect considers the least number of elements possible Early out from TSet Contains if Other is larger than this Clarify comment on TSet Difference #rb Steve.Robb Change 2841380 on 2016/01/24 by Aaron.McLeran UE-25586 Audio assets not correctly reporting resource memory usage Tested on PC/PS4 and with Editor builds. Memory reporting is working for all cases now. Change 2841385 on 2016/01/24 by Aaron.McLeran UE-21210 Adding subtitle priority to USoundWave Change 2841386 on 2016/01/24 by Marc.Audy Return null for GameNetDriver if World is null instead of crashing Change 2841409 on 2016/01/24 by Aaron.McLeran UE-25514 Removing load for default objects for every sound wave Change 2841858 on 2016/01/25 by Ori.Cohen Make sure that PIE face index results are consistent with runtime #rb Benn.Gallagher Change 2841977 on 2016/01/25 by Ori.Cohen Fix object type customization so that it's only enabled when custom is selected. (Accidently broke this in recent change) Change 2841982 on 2016/01/25 by Marc.Audy Minor optimization by avoiding recreating FNames repeatedly in constructor Change 2842169 on 2016/01/25 by Benn.Gallagher Fixes to animBP compiler and instance to store and double buffer internal machine state weights on the instance. So they can be queried cross-machine without issue. #rb Lina.Halper Change 2842390 on 2016/01/25 by Ori.Cohen Fix in world editing of BodyInstance not working. No longer serializing Scale3D as this is allways initialized in InitBody. No longer overwriting MassInKg and renamed to to MassInKgOverride which better reflects what this variable does. #JIRA UE-25518 #rb Lina.Halper Change 2843579 on 2016/01/26 by Marc.Audy Only update replication when it actually changes Don't check calling SetIsReplicated if the class cannot replicate, instead output an error message Fix spelling in comment #rb Ori.Cohen Change 2843627 on 2016/01/26 by Marc.Audy Add \\ as a default console key for Italian keyboard layouts #jira UE-25198 #rb James.Golding Change 2843628 on 2016/01/26 by Marc.Audy Don't reconstruct FName on each call to GetHitResultAtScreenPosition #rb James.Golding Change 2843671 on 2016/01/26 by Martin.Wilson Fix incorrect bone transforms being pushed to the renderer during SetSkeletalMesh. This presented as motion blur artifacts in editor #rb Thomas.Sarkanen Change 2843768 on 2016/01/26 by Marc.Audy Inline Get Component functions in TriggerBase Change 2844003 on 2016/01/26 by Zak.Middleton #ue4 - Fix FMath::Fmod(X, Y) sometimes returning small negative values for positive X and Y due to float imprecision. Added tests to math tests at startup to check this, and also to better handle results close to Y. Wrap the ensure on Y=0 within a conditional so a breakpoint can be used during debugging (to distinguish between zero and very small input). #codereview Laurent.Delayen Change 2844005 on 2016/01/26 by Zak.Middleton #ue4 - Convert uses of fmod() and fmodf() to use FMath::Fmod() instead. Also see CL 2844003 [CL 2855709 by Marc Audy in Main branch]
2016-02-04 10:55:30 -05:00
// suppress the run command because it can be long and intimidating, making the logs around this code harder to read.
var Result = CommandUtils.Run(CommandUtils.CmdEnv.MsBuildExe, CmdLine, Options: CommandUtils.ERunOptions.Default | CommandUtils.ERunOptions.NoLoggingOfRunCommand | CommandUtils.ERunOptions.LoggingOfRunDuration);
if (Result.ExitCode != 0)
{
throw new AutomationException(String.Format("Failed to build \"{0}\":{1}{2}", UATProj, Environment.NewLine, Result.Output));
}
}
else
{
CompileModules(ModulesToCompile);
}
Environment.CurrentDirectory = OldCWD;
}
/// <summary>
/// Compiles all script modules.
/// </summary>
/// <param name="Modules">Module project filenames.</param>
/// <param name="CompiledModuleFilenames">The resulting compiled module assembly filenames.</param>
private static void CompileModules(List<string> Modules)
{
Log.TraceInformation("Building script modules");
// Make sure DefaultScriptsDLLName is compiled first
var DefaultScriptsProjName = Path.ChangeExtension(DefaultScriptsDLLName, "csproj");
foreach (var ModuleName in Modules)
{
if (ModuleName.IndexOf(DefaultScriptsProjName, StringComparison.InvariantCultureIgnoreCase) >= 0)
{
Log.TraceInformation("Building script module: {0}", ModuleName);
try
{
CompileScriptModule(ModuleName);
}
catch (Exception Ex)
{
CommandUtils.LogError(LogUtils.FormatException(Ex));
throw new AutomationException("Failed to compile module {0}", ModuleName);
}
break;
}
}
// Second pass, compile everything else
foreach (var ModuleName in Modules)
{
if (ModuleName.IndexOf(DefaultScriptsProjName, StringComparison.InvariantCultureIgnoreCase) < 0)
{
Log.TraceInformation("Building script module: {0}", ModuleName);
try
{
CompileScriptModule(ModuleName);
}
catch (Exception Ex)
{
CommandUtils.LogError(LogUtils.FormatException(Ex));
throw new AutomationException("Failed to compile module {0}", ModuleName);
}
}
}
}
/// <summary>
/// Builds a script module (csproj file)
/// </summary>
/// <param name="ProjectFile"></param>
/// <returns></returns>
private static bool CompileScriptModule(string ProjectFile)
{
if (!ProjectFile.EndsWith(".csproj", StringComparison.InvariantCultureIgnoreCase))
{
throw new AutomationException(String.Format("Unable to build Project {0}. Not a valid .csproj file.", ProjectFile));
}
if (!CommandUtils.FileExists(ProjectFile))
{
throw new AutomationException(String.Format("Unable to build Project {0}. Project file not found.", ProjectFile));
}
var CmdLine = String.Format("\"{0}\" /verbosity:quiet /nologo /target:Build /property:Configuration={1} /property:Platform=AnyCPU /p:TreatWarningsAsErrors=false /p:NoWarn=\"612,618,672\" /p:BuildProjectReferences=true",
ProjectFile, BuildConfig);
// Compile the project
var Result = CommandUtils.Run(CommandUtils.CmdEnv.MsBuildExe, CmdLine);
if (Result.ExitCode != 0)
{
throw new AutomationException(String.Format("Failed to build \"{0}\":{1}{2}", ProjectFile, Environment.NewLine, Result.Output));
}
else
{
// Remove .Automation.csproj and copy to target dir
Log.TraceVerbose("Successfully compiled {0}", ProjectFile);
}
return Result.ExitCode == 0;
}
/// <summary>
/// Loads all precompiled assemblies (DLLs that end with *Scripts.dll).
/// </summary>
/// <param name="OutScriptAssemblies">List to store all loaded assemblies.</param>
private static void LoadPreCompiledScriptAssemblies(List<Assembly> OutScriptAssemblies)
{
CommandUtils.LogVerbose("Loading precompiled script DLLs");
bool DefaultScriptsDLLFound = false;
var ScriptsLocation = GetScriptAssemblyFolder();
if (CommandUtils.DirectoryExists(ScriptsLocation))
{
var ScriptDLLFiles = Directory.GetFiles(ScriptsLocation, "*.Automation.dll", SearchOption.AllDirectories);
CommandUtils.LogVerbose("Found {0} script DLL(s).", ScriptDLLFiles.Length);
foreach (var ScriptsDLLFilename in ScriptDLLFiles)
{
if (!HostPlatform.Current.IsScriptModuleSupported(CommandUtils.GetFilenameWithoutAnyExtensions(ScriptsDLLFilename)))
{
CommandUtils.LogVerbose("Script module {0} filtered by the Host Platform and will not be loaded.", ScriptsDLLFilename);
continue;
}
// Load the assembly into our app domain
CommandUtils.LogVerbose("Loading script DLL: {0}", ScriptsDLLFilename);
try
{
var Dll = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(ScriptsDLLFilename));
OutScriptAssemblies.Add(Dll);
// Check if this is the default scripts DLL.
if (!DefaultScriptsDLLFound && String.Compare(Path.GetFileName(ScriptsDLLFilename), DefaultScriptsDLLName, true) == 0)
{
DefaultScriptsDLLFound = true;
}
}
catch (Exception Ex)
{
throw new AutomationException("Failed to load script DLL: {0}: {1}", ScriptsDLLFilename, Ex.Message);
}
}
}
else
{
CommandUtils.LogError("Scripts folder {0} does not exist!", ScriptsLocation);
}
// The default scripts DLL is required!
if (!DefaultScriptsDLLFound)
{
throw new AutomationException("{0} was not found or could not be loaded, can't run scripts.", DefaultScriptsDLLName);
}
}
private void CleanupScriptsAssemblies()
{
CommandUtils.LogVerbose("Cleaning up script DLL folder");
CommandUtils.DeleteDirectory(GetScriptAssemblyFolder());
}
private static string GetScriptAssemblyFolder()
{
return CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "Engine", "Binaries", "DotNET", "AutomationScripts");
}
#endregion
#region Properties
public CaselessDictionary<Type> Commands
{
get { return ScriptCommands; }
}
#endregion
}
}