// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using AutomationTool.Tasks;
using EpicGames.BuildGraph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
using UnrealBuildTool;
using Microsoft.Extensions.Logging;
using static AutomationTool.CommandUtils;
namespace AutomationTool.Tasks
{
///
/// Parameters for a task that strips symbols from a set of files
///
public class StripTaskParameters
{
///
/// The platform toolchain to strip binaries.
///
[TaskParameter]
public UnrealTargetPlatform Platform;
///
/// The directory to find files in.
///
[TaskParameter(Optional = true)]
public DirectoryReference BaseDir;
///
/// List of file specifications separated by semicolons (for example, Engine/.../*.pdb), or the name of a tag set.
///
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string Files;
///
/// Output directory for the stripped files. Defaults to the input path, overwriting the input files.
///
[TaskParameter(Optional = true)]
public DirectoryReference OutputDir;
///
/// Tag to be applied to build products of this task.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag;
}
///
/// Strips debugging information from a set of files.
///
[TaskElement("Strip", typeof(StripTaskParameters))]
public class StripTask : BgTaskImpl
{
///
/// Parameters for this task
///
StripTaskParameters Parameters;
///
/// Construct a spawn task
///
/// Parameters for the task
public StripTask(StripTaskParameters InParameters)
{
Parameters = InParameters;
}
///
/// Execute the task.
///
/// Information about the current job
/// Set of build products produced by this node.
/// Mapping from tag names to the set of files they include
public override Task ExecuteAsync(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet)
{
// Get the base directory
DirectoryReference BaseDir = Parameters.BaseDir;
// Get the output directory
DirectoryReference OutputDir = Parameters.OutputDir;
// Find the matching files
FileReference[] SourceFiles = ResolveFilespec(BaseDir, Parameters.Files, TagNameToFileSet).OrderBy(x => x.FullName).ToArray();
// Create the matching target files
FileReference[] TargetFiles = SourceFiles.Select(x => FileReference.Combine(OutputDir, x.MakeRelativeTo(BaseDir))).ToArray();
// Run the stripping command
Platform TargetPlatform = Platform.GetPlatform(Parameters.Platform);
for (int Idx = 0; Idx < SourceFiles.Length; Idx++)
{
DirectoryReference.CreateDirectory(TargetFiles[Idx].Directory);
if (SourceFiles[Idx] == TargetFiles[Idx])
{
Logger.LogInformation("Stripping symbols: {Arg0}", SourceFiles[Idx].FullName);
}
else
{
Logger.LogInformation("Stripping symbols: {Arg0} -> {Arg1}", SourceFiles[Idx].FullName, TargetFiles[Idx].FullName);
}
TargetPlatform.StripSymbols(SourceFiles[Idx], TargetFiles[Idx]);
}
// Apply the optional tag to the build products
foreach(string TagName in FindTagNamesFromList(Parameters.Tag))
{
FindOrAddTagSet(TagNameToFileSet, TagName).UnionWith(TargetFiles);
}
// Add the target files to the set of build products
BuildProducts.UnionWith(TargetFiles);
return Task.CompletedTask;
}
///
/// Output this task out to an XML writer.
///
public override void Write(XmlWriter Writer)
{
Write(Writer, Parameters);
}
///
/// Find all the tags which are used as inputs to this task
///
/// The tag names which are read by this task
public override IEnumerable FindConsumedTagNames()
{
return FindTagNamesFromFilespec(Parameters.Files);
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
return FindTagNamesFromList(Parameters.Tag);
}
}
public static partial class StandardTasks
{
///
/// Strips symbols from a set of files.
///
///
/// The platform toolchain to strip binaries.
/// The directory to find files in.
/// Output directory for the stripped files. Defaults to the input path, overwriting the input files.
///
public static async Task StripAsync(FileSet Files, UnrealTargetPlatform Platform, DirectoryReference BaseDir = null, DirectoryReference OutputDir = null)
{
StripTaskParameters Parameters = new StripTaskParameters();
Parameters.Platform = Platform;
Parameters.BaseDir = BaseDir;
Parameters.Files = String.Join(";", Files.Flatten().Values.Select(x => x.FullName));
Parameters.OutputDir = OutputDir;
return await ExecuteAsync(new StripTask(Parameters));
}
}
}