コード例 #1
0
        /// <summary>
        /// Main entry point. Parses any global options and initializes the logging system, then invokes the appropriate command.
        /// </summary>
        /// <param name="ArgumentsArray">Command line arguments</param>
        /// <returns>Zero on success, non-zero on error</returns>
        private static int Main(string[] ArgumentsArray)
        {
            SingleInstanceMutex Mutex = null;

            try
            {
                // Start capturing performance info
                Timeline.Start();

                // Parse the command line arguments
                CommandLineArguments Arguments = new CommandLineArguments(ArgumentsArray);

                // Parse the global options
                GlobalOptions Options = new GlobalOptions(Arguments);

                // Configure the log system
                Log.OutputLevel       = Options.LogOutputLevel;
                Log.IncludeTimestamps = Options.bLogTimestamps;
                Log.IncludeProgramNameWithSeverityPrefix = Options.bLogFromMsBuild;

                // Configure the progress writer
                ProgressWriter.bWriteMarkup = Options.bWriteProgressMarkup;

                // Add the log writer if requested. When building a target, we'll create the writer for the default log file later.
                if (Options.LogFileName != null)
                {
                    Log.AddFileWriter("LogTraceListener", Options.LogFileName);
                }

                // Ensure we can resolve any external assemblies that are not in the same folder as our assembly.
                AssemblyUtils.InstallAssemblyResolver(Path.GetDirectoryName(Assembly.GetEntryAssembly().GetOriginalLocation()));

                // Change the working directory to be the Engine/Source folder. We are likely running from Engine/Binaries/DotNET
                // This is critical to be done early so any code that relies on the current directory being Engine/Source will work.
                DirectoryReference.SetCurrentDirectory(UnrealBuildTool.EngineSourceDirectory);

                // Get the type of the mode to execute, using a fast-path for the build mode.
                Type ModeType = typeof(BuildMode);
                if (Options.Mode != null)
                {
                    // Find all the valid modes
                    Dictionary <string, Type> ModeNameToType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
                    foreach (Type Type in Assembly.GetExecutingAssembly().GetTypes())
                    {
                        if (Type.IsClass && !Type.IsAbstract && Type.IsSubclassOf(typeof(ToolMode)))
                        {
                            ToolModeAttribute Attribute = Type.GetCustomAttribute <ToolModeAttribute>();
                            if (Attribute == null)
                            {
                                throw new BuildException("Class '{0}' should have a ToolModeAttribute", Type.Name);
                            }
                            ModeNameToType.Add(Attribute.Name, Type);
                        }
                    }

                    // Try to get the correct mode
                    if (!ModeNameToType.TryGetValue(Options.Mode, out ModeType))
                    {
                        Log.TraceError("No mode named '{0}'. Available modes are:\n  {1}", Options.Mode, String.Join("\n  ", ModeNameToType.Keys));
                        return(1);
                    }
                }

                // Get the options for which systems have to be initialized for this mode
                ToolModeOptions ModeOptions = ModeType.GetCustomAttribute <ToolModeAttribute>().Options;

                // Start prefetching the contents of the engine folder
                if ((ModeOptions & ToolModeOptions.StartPrefetchingEngine) != 0)
                {
                    using (Timeline.ScopeEvent("FileMetadataPrefetch.QueueEngineDirectory()"))
                    {
                        FileMetadataPrefetch.QueueEngineDirectory();
                    }
                }

                // Read the XML configuration files
                if ((ModeOptions & ToolModeOptions.XmlConfig) != 0)
                {
                    using (Timeline.ScopeEvent("XmlConfig.ReadConfigFiles()"))
                    {
                        string XmlConfigMutexName = SingleInstanceMutex.GetUniqueMutexForPath("UnrealBuildTool_Mutex_XmlConfig", Assembly.GetExecutingAssembly().CodeBase);
                        using (SingleInstanceMutex XmlConfigMutex = new SingleInstanceMutex(XmlConfigMutexName, true))
                        {
                            FileReference XmlConfigCache = Arguments.GetFileReferenceOrDefault("-XmlConfigCache=", null);
                            XmlConfig.ReadConfigFiles(XmlConfigCache);
                        }
                    }
                }

                // Acquire a lock for this branch
                if ((ModeOptions & ToolModeOptions.SingleInstance) != 0 && !Options.bNoMutex)
                {
                    using (Timeline.ScopeEvent("SingleInstanceMutex.Acquire()"))
                    {
                        string MutexName = SingleInstanceMutex.GetUniqueMutexForPath("UnrealBuildTool_Mutex", Assembly.GetExecutingAssembly().CodeBase);
                        Mutex = new SingleInstanceMutex(MutexName, Options.bWaitMutex);
                    }
                }

                // Register all the build platforms
                if ((ModeOptions & ToolModeOptions.BuildPlatforms) != 0)
                {
                    using (Timeline.ScopeEvent("UEBuildPlatform.RegisterPlatforms()"))
                    {
                        UEBuildPlatform.RegisterPlatforms(false);
                    }
                }
                if ((ModeOptions & ToolModeOptions.BuildPlatformsForValidation) != 0)
                {
                    using (Timeline.ScopeEvent("UEBuildPlatform.RegisterPlatforms()"))
                    {
                        UEBuildPlatform.RegisterPlatforms(true);
                    }
                }

                // Create the appropriate handler
                ToolMode Mode = (ToolMode)Activator.CreateInstance(ModeType);

                // Execute the mode
                int Result = Mode.Execute(Arguments);
                if ((ModeOptions & ToolModeOptions.ShowExecutionTime) != 0)
                {
                    Log.TraceInformation("Total execution time: {0:0.00} seconds", Timeline.Elapsed.TotalSeconds);
                }
                return(Result);
            }
            catch (CompilationResultException Ex)
            {
                // Used to return a propagate a specific exit code after an error has occurred. Does not log any message.
                Log.TraceLog(ExceptionUtils.FormatExceptionDetails(Ex));
                return((int)Ex.Result);
            }
            catch (BuildException Ex)
            {
                // BuildExceptions should have nicely formatted messages. We can log these directly.
                Log.TraceError(ExceptionUtils.FormatException(Ex));
                Log.TraceLog(ExceptionUtils.FormatExceptionDetails(Ex));
                return((int)CompilationResult.OtherCompilationError);
            }
            catch (Exception Ex)
            {
                // Unhandled exception.
                Log.TraceError("Unhandled exception: {0}", ExceptionUtils.FormatException(Ex));
                Log.TraceLog(ExceptionUtils.FormatExceptionDetails(Ex));
                return((int)CompilationResult.OtherCompilationError);
            }
            finally
            {
                // Cancel the prefetcher
                using (Timeline.ScopeEvent("FileMetadataPrefetch.Stop()"))
                {
                    FileMetadataPrefetch.Stop();
                }

                // Print out all the performance info
                Timeline.Print(TimeSpan.FromMilliseconds(20.0), LogEventType.Log);

                // Make sure we flush the logs however we exit
                Trace.Close();

                // Dispose of the mutex. Must be done last to ensure that another process does not startup and start trying to write to the same log file.
                if (Mutex != null)
                {
                    Mutex.Dispose();
                }
            }
        }