Skip to content

Commit

Permalink
Fea #5, Ld任务支持最小化生成
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Apr 28, 2023
1 parent e07f212 commit fcdc09a
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 51 deletions.
12 changes: 6 additions & 6 deletions Microsoft.Build.CPPTasks/VCToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ protected virtual bool GenerateCostomCommandsAccordingToType(CommandLineBuilder
CommandLineBuilder commandLineBuilder = new CommandLineBuilder(quoteHyphensOnCommandLine: true);
foreach (string switchOrder in SwitchOrderList)
{
if (IsPropertySet(switchOrder))
if (GenerateCostomCommandsAccordingToType(commandLineBuilder, switchOrder, dummyForBackwardCompatibility: false, format, escapeFormat))
{
// 已经处理
}
else if(IsPropertySet(switchOrder))
{
ToolSwitch toolSwitch = activeToolSwitches[switchOrder];
if (!VerifyDependenciesArePresent(toolSwitch) || !VerifyRequiredArgumentsArePresent(toolSwitch, throwOnError: false))
Expand All @@ -313,11 +317,7 @@ protected virtual bool GenerateCostomCommandsAccordingToType(CommandLineBuilder
{
GenerateCommandsAccordingToType(commandLineBuilder, toolSwitch, dummyForBackwardCompatibility: false, format, escapeFormat);
}
}
else if(GenerateCostomCommandsAccordingToType(commandLineBuilder, switchOrder, dummyForBackwardCompatibility : false, format, escapeFormat))
{
// 已经处理
}
}
else if (string.Equals(switchOrder, "additionaloptions", StringComparison.OrdinalIgnoreCase))
{
BuildAdditionalArgs(commandLineBuilder);
Expand Down
8 changes: 8 additions & 0 deletions YY.Build.Linux.Tasks/GCC/Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,14 @@ public virtual string TrackerLogDirectory
}
}

protected override string CommandTLogName
{
get
{
return "compile." + base.CommandTLogName;
}
}

protected bool InputDependencyFilter(string fullInputPath)
{
if (fullInputPath.EndsWith(".PDB", StringComparison.OrdinalIgnoreCase) || fullInputPath.EndsWith(".IDB", StringComparison.OrdinalIgnoreCase))
Expand Down
153 changes: 152 additions & 1 deletion YY.Build.Linux.Tasks/GCC/Ld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
// using Microsoft.Build.Linux.Tasks;
using Microsoft.Build.Utilities;
using System.Text.RegularExpressions;
using Microsoft.Build.Shared;
using System.IO;

namespace YY.Build.Linux.Tasks.GCC
{
public class Ld : Microsoft.Build.CPPTasks.VCToolTask
public class Ld : TrackedVCToolTask
{
public Ld()
: base(Microsoft.Build.CppTasks.Common.Properties.Microsoft_Build_CPPTasks_Strings.ResourceManager)
Expand Down Expand Up @@ -60,6 +62,18 @@ public Ld()

protected override string ToolName => "ld";

protected override ITaskItem[] TrackedInputFiles => Sources;

protected override bool MaintainCompositeRootingMarkers => true;

protected override string CommandTLogName
{
get
{
return "ld." + base.CommandTLogName;
}
}

public string OutputFile
{
get
Expand Down Expand Up @@ -764,5 +778,142 @@ public virtual string UseOfStl
AddActiveSwitchToolValue(toolSwitch);
}
}

protected override string TrackerIntermediateDirectory
{
get
{
if (TrackerLogDirectory != null)
{
return TrackerLogDirectory;
}
return string.Empty;
}
}

public virtual string TrackerLogDirectory
{
get
{
if (IsPropertySet("TrackerLogDirectory"))
{
return base.ActiveToolSwitches["TrackerLogDirectory"].Value;
}
return null;
}
set
{
base.ActiveToolSwitches.Remove("TrackerLogDirectory");
ToolSwitch toolSwitch = new ToolSwitch(ToolSwitchType.Directory);
toolSwitch.DisplayName = "Tracker Log Directory";
toolSwitch.Description = "Tracker log directory.";
toolSwitch.ArgumentRelationList = new ArrayList();
toolSwitch.Value = VCToolTask.EnsureTrailingSlash(value);
base.ActiveToolSwitches.Add("TrackerLogDirectory", toolSwitch);
AddActiveSwitchToolValue(toolSwitch);
}
}

protected override bool GenerateCostomCommandsAccordingToType(CommandLineBuilder builder, string switchName, bool dummyForBackwardCompatibility, CommandLineFormat format = CommandLineFormat.ForBuildLog, EscapeFormat escapeFormat = EscapeFormat.Default)
{
if (MinimalRebuildFromTracking == true && string.Equals(switchName, "Trace", StringComparison.OrdinalIgnoreCase))
{
// MinimalRebuildFromTracking开启时,强制将 Trace 认为是开启的。
ToolSwitch toolSwitch = new ToolSwitch(ToolSwitchType.Boolean);
toolSwitch.DisplayName = "Trace";
toolSwitch.Description = "The --trace option tells the linker to output the input files as are processed.";
toolSwitch.ArgumentRelationList = new ArrayList();
toolSwitch.SwitchValue = "-Wl,--trace";
toolSwitch.Name = "Trace";
toolSwitch.BooleanValue = true;

GenerateCommandsAccordingToType(builder, toolSwitch, format, escapeFormat);
return true;
}
return false;
}

private HashSet<string> ReadObjectFiles;

protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
ReadObjectFiles = null;

if (MinimalRebuildFromTracking == true)
{
ReadObjectFiles = new HashSet<string>();
}

return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
}

protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
do
{
if (StandardOutputImportanceToUse != messageImportance)
break;

if (ReadObjectFiles == null)
break;

if (singleLine.Length == 0)
break;

if(singleLine[0] != '/' || singleLine.Contains(": "))
break;

try
{
var ObjPath = FileUtilities.NormalizePath(singleLine);
ReadObjectFiles.Add(ObjPath);
// Trace如果本身没有开启,那么不向其输出搜索内容,避免内容太多形成干扰。
if (!Trace)
return;
} catch
{
}

} while (false);

base.LogEventsFromTextOutput(singleLine, messageImportance);
}

protected override void SaveTracking()
{
string SourceKey = "^";

foreach (ITaskItem taskItem in Sources)
{
if (SourceKey.Length != 1)
SourceKey += '|';

SourceKey += FileTracker.FormatRootingMarker(taskItem);
}

// 保存Write文件
{
string WriteFilePath = TLogWriteFiles[0].GetMetadata("FullPath");
Directory.CreateDirectory(Path.GetDirectoryName(WriteFilePath));
using StreamWriter WriteFileWriter = FileUtilities.OpenWrite(WriteFilePath, append: true, Encoding.Unicode);

WriteFileWriter.WriteLine(SourceKey);
WriteFileWriter.WriteLine(OutputFile);
}

// 保存Read文件
{
string ReadFilePath = TLogReadFiles[0].GetMetadata("FullPath");
Directory.CreateDirectory(Path.GetDirectoryName(ReadFilePath));
using StreamWriter ReadFileWriter = FileUtilities.OpenWrite(ReadFilePath, append: true, Encoding.Unicode);

ReadFileWriter.WriteLine(SourceKey);

foreach (var FilePath in ReadObjectFiles)
{
ReadFileWriter.WriteLine(FilePath);
}
}
}
}
}
1 change: 1 addition & 0 deletions YY.Build.Linux.Tasks/Targets/YY.Linux.Cross.props
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<FunctionBinding Condition="'$(PlatformTarget)' == 'x86'">false</FunctionBinding>
<NoExecStackRequired>true</NoExecStackRequired>
<LinkDLL Condition="'$(ConfigurationType)' == 'DynamicLibrary'">true</LinkDLL>
<MinimalRebuildFromTracking>true</MinimalRebuildFromTracking>
</Link>
<Lib>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
Expand Down
89 changes: 45 additions & 44 deletions YY.Build.Linux.Tasks/Targets/YY.Linux.Cross.targets
Original file line number Diff line number Diff line change
Expand Up @@ -154,50 +154,51 @@ Copyright (C) Microsoft Corporation. All rights reserved.
</Target>

<Target Name="Link">
<ItemGroup>
<Link>
<TimestampSurrogate Condition="'%(Link.TimestampSurrogate)' == ''">$(CompileUpToDateFile)</TimestampSurrogate>
</Link>
</ItemGroup>
<Ld Condition="'@(Link)' != ''"
Sources="@(Link)"
OutputFile="%(Link.OutputFile)"
ShowProgress="%(Link.ShowProgress)"
Version="%(Link.Version)"
VerboseOutput="%(Link.VerboseOutput)"
Trace="%(Link.Trace)"
TraceSymbols="%(Link.TraceSymbols)"
PrintMap="%(Link.PrintMap)"
UnresolvedSymbolReferences="%(Link.UnresolvedSymbolReferences)"
OptimizeforMemory="%(Link.OptimizeforMemory)"
SharedLibrarySearchPath="%(Link.SharedLibrarySearchPath)"
AdditionalLibraryDirectories="%(Link.AdditionalLibraryDirectories)"
IgnoreSpecificDefaultLibraries="%(Link.IgnoreSpecificDefaultLibraries)"
IgnoreDefaultLibraries="%(Link.IgnoreDefaultLibraries)"
ForceUndefineSymbolReferences="%(Link.ForceUndefineSymbolReferences)"
DebuggerSymbolInformation="%(Link.DebuggerSymbolInformation)"
GenerateMapFile="%(Link.GenerateMapFile)"
Relocation="%(Link.Relocation)"
FunctionBinding="%(Link.FunctionBinding)"
NoExecStackRequired="%(Link.NoExecStackRequired)"
LinkDll="%(Link.LinkDll)"
WholeArchiveBegin="%(Link.WholeArchiveBegin)"
AdditionalOptions="%(Link.AdditionalOptions)"
AdditionalDependencies="%(Link.AdditionalDependencies)"
WholeArchiveEnd="%(Link.WholeArchiveEnd)"
LibraryDependencies="%(Link.LibraryDependencies)"
BuildingInIde="%(Link.BuildingInIde)"
EnableASAN="%(Link.EnableASAN)"
UseOfStl="%(Link.UseOfStl)"
ToolExe="$(RemoteLdToolExe)"
>
</Ld>

<Message Text="$(MSBuildProjectFile) -&gt; $(TargetPath)" Importance="High"/>

<WriteLinesToFile Overwrite="true" Condition="'@(ProjectReferenceToLink->HasMetadata(TimestampSurrogate))' != ''" Encoding="Unicode" File="$(TLogLocation)\link.read.1.tlog" Lines="@(Link->'^%(Fullpath)','|');@(ProjectReferenceToLink->'%(TimestampSurrogate)'->Distinct())" />

<WriteLinesToFile Overwrite="true" Encoding="Unicode" File="$(TLogLocation)\link.write.1.tlog" Lines="@(Link->'^%(Fullpath)','|');$(LinkUpToDateFile)" />
<PropertyGroup>
<Link_MinimalRebuildFromTracking Condition="'@(Link->AnyHaveMetadataValue('MinimalRebuildFromTracking', 'false'))' == 'true'">false</Link_MinimalRebuildFromTracking>
<Link_MinimalRebuildFromTracking Condition="'$(_BuildActionType)' != 'Build' or '$(ForceRebuild)' == 'true'" >false</Link_MinimalRebuildFromTracking>
<Link_MinimalRebuildFromTracking Condition="'$(Link_MinimalRebuildFromTracking)' == ''">true</Link_MinimalRebuildFromTracking>
</PropertyGroup>
<Ld Condition="'@(Link)' != ''"
Sources="@(Link)"
OutputFile="%(Link.OutputFile)"
ShowProgress="%(Link.ShowProgress)"
Version="%(Link.Version)"
VerboseOutput="%(Link.VerboseOutput)"
Trace="%(Link.Trace)"
TraceSymbols="%(Link.TraceSymbols)"
PrintMap="%(Link.PrintMap)"
UnresolvedSymbolReferences="%(Link.UnresolvedSymbolReferences)"
OptimizeforMemory="%(Link.OptimizeforMemory)"
SharedLibrarySearchPath="%(Link.SharedLibrarySearchPath)"
AdditionalLibraryDirectories="%(Link.AdditionalLibraryDirectories)"
IgnoreSpecificDefaultLibraries="%(Link.IgnoreSpecificDefaultLibraries)"
IgnoreDefaultLibraries="%(Link.IgnoreDefaultLibraries)"
ForceUndefineSymbolReferences="%(Link.ForceUndefineSymbolReferences)"
DebuggerSymbolInformation="%(Link.DebuggerSymbolInformation)"
GenerateMapFile="%(Link.GenerateMapFile)"
Relocation="%(Link.Relocation)"
FunctionBinding="%(Link.FunctionBinding)"
NoExecStackRequired="%(Link.NoExecStackRequired)"
LinkDll="%(Link.LinkDll)"
WholeArchiveBegin="%(Link.WholeArchiveBegin)"
AdditionalOptions="%(Link.AdditionalOptions)"
AdditionalDependencies="%(Link.AdditionalDependencies)"
WholeArchiveEnd="%(Link.WholeArchiveEnd)"
LibraryDependencies="%(Link.LibraryDependencies)"
BuildingInIde="%(Link.BuildingInIde)"
EnableASAN="%(Link.EnableASAN)"
UseOfStl="%(Link.UseOfStl)"
TrackerLogDirectory="$(TLogLocation)"
TLogReadFiles="$(TLogLocation)\link.read.1.tlog"
TLogWriteFiles="$(TLogLocation)\link.write.1.tlog"
TrackFileAccess ="$(TrackFileAccess)"
MinimalRebuildFromTracking ="$(Link_MinimalRebuildFromTracking)"
ToolExe="$(RemoteLdToolExe)"
>
<Output TaskParameter="SkippedExecution" PropertyName="LinkSkippedExecution" />
</Ld>
<Message Text="$(MSBuildProjectFile) -&gt; $(TargetPath)" Importance="High"/>
</Target>

<Target Name="Lib">
Expand Down

0 comments on commit fcdc09a

Please sign in to comment.