Skip to content

Commit 4f8622a

Browse files
ViktorHoferbaronfel
authored andcommitted
Reduce disk space usage by using hard links in a few copy operations and improve repo clean (dotnet#46208)
1 parent b3bf5ae commit 4f8622a

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

src/SourceBuild/content/Directory.Build.targets

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
Inputs="@(BinPlaceDir);%(BinPlaceDir.Identity)"
4646
Outputs="unused">
4747
<PropertyGroup>
48-
<BinPlaceUseHardlinksIfPossible Condition="'$(BinPlaceUseHardlinksIfPossible)' == ''">true</BinPlaceUseHardlinksIfPossible>
4948
<_BinPlaceDir>%(BinPlaceDir.Identity)</_BinPlaceDir>
5049
</PropertyGroup>
5150

@@ -62,7 +61,7 @@
6261
OverwriteReadOnlyFiles="true"
6362
Retries="$(CopyRetryCount)"
6463
RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
65-
UseHardlinksIfPossible="$(BinPlaceUseHardlinksIfPossible)" />
64+
UseHardlinksIfPossible="true" />
6665
</Target>
6766

6867
</Project>

src/SourceBuild/content/repo-projects/Directory.Build.targets

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@
129129

130130
<MakeDir Directories="$([System.IO.Path]::GetDirectoryName('$(NuGetConfigFile)'))" />
131131

132-
<Copy
133-
SourceFiles="$(OriginalNuGetConfigFile)"
134-
DestinationFiles="$(NuGetConfigFile)">
132+
<Copy SourceFiles="$(OriginalNuGetConfigFile)"
133+
DestinationFiles="$(NuGetConfigFile)">
135134
<Output TaskParameter="CopiedFiles" ItemName="FileWrites" />
136135
</Copy>
137136
</Target>
@@ -602,6 +601,7 @@
602601
DestinationFiles="$(NuGetPackageRoot)%(RecursiveDir)%(Filename)%(Extension)"
603602
SkipUnchangedFiles="true"
604603
Retries="5"
604+
UseHardlinksIfPossible="true"
605605
Condition="'@(_InnerPackageCacheFiles)' != ''" />
606606
</Target>
607607

@@ -625,9 +625,28 @@
625625
Condition="'@(PrebuiltReportsToMove)' != ''" />
626626
</Target>
627627

628+
<!-- Make a copy of project.assets.json files for prebuilt report generation. -->
629+
<Target Name="BackupProjectAssetsJsonFiles"
630+
Condition="'$(DotNetBuildSourceOnly)' == 'true'">
631+
<PropertyGroup>
632+
<ProjectAssetsJsonBackupDir>$([MSBuild]::NormalizeDirectory('$(BaseIntermediateOutputPath)', 'project-assets-json'))</ProjectAssetsJsonBackupDir>
633+
</PropertyGroup>
634+
635+
<ItemGroup>
636+
<ProjectAssetsJsonFile Include="$(RepoArtifactsDir)**/project.assets.json" />
637+
</ItemGroup>
638+
639+
<Copy SourceFiles="@(ProjectAssetsJsonFile)"
640+
DestinationFolder="$(ProjectAssetsJsonBackupDir)%(RecursiveDir)"
641+
UseHardlinksIfPossible="true"
642+
SkipUnchangedFiles="true"
643+
Condition="'@(ProjectAssetsJsonFile)' != ''" />
644+
</Target>
645+
628646
<Target Name="CleanupRepo"
629647
Inputs="$(MSBuildProjectFullPath)"
630648
Outputs="$(BaseIntermediateOutputPath)CleanupRepo.complete"
649+
DependsOnTargets="BackupProjectAssetsJsonFiles"
631650
Condition="'$(IsUtilityProject)' != 'true' and
632651
'$(CleanWhileBuilding)' == 'true' and
633652
Exists('$(RepoArtifactsDir)')">
@@ -646,35 +665,20 @@
646665
IgnoreStandardErrorWarningFormat="true"
647666
IgnoreExitCode="true" />
648667

668+
<Message Text="DirSize After Building $(RepositoryName)" Importance="High" Condition="'$(BuildOS)' != 'windows'" />
669+
<Exec Command="df -h $(RepoRoot)" Condition="'$(BuildOS)' != 'windows'" />
670+
671+
<!-- Cleanup the entire repo artifacts dir. Ignore failures as this is best effort.
672+
Don't use Removedir as ContinueOnError on it doesn't work when using warnaserror/TreatWarningsAsErrors. -->
649673
<PropertyGroup>
650-
<BuildObjDir>$([MSBuild]::NormalizeDirectory('$(ProjectDirectory)', 'artifacts', 'buildObj'))</BuildObjDir>
674+
<RepoCleanCommand Condition="'$(BuildOS)' == 'windows'">rmdir "$(RepoArtifactsDir.TrimEnd('\'))" /s /q</RepoCleanCommand>
675+
<RepoCleanCommand Condition="'$(BuildOS)' != 'windows'">rm -rf "$(RepoArtifactsDir)"</RepoCleanCommand>
651676
</PropertyGroup>
652677

653-
<ItemGroup>
654-
<ObjFilesToCopy Include="$(RepoArtifactsDir)**/project.assets.json" />
655-
</ItemGroup>
656-
657-
<!-- Make a copy of project.assets.json files -->
658-
<Copy SourceFiles="@(ObjFilesToCopy)"
659-
DestinationFolder="$(BuildObjDir)%(RecursiveDir)"
660-
Condition="'@(ObjFilesToCopy)' != ''" />
661-
662-
<ItemGroup>
663-
<DirsToDelete Include="$([System.IO.Directory]::GetDirectories('$(RepoArtifactsDir)'))" />
664-
665-
<DirsToDeleteWithTrailingSeparator Include="$([MSBuild]::EnsureTrailingSlash('%(DirsToDelete.Identity)'))" />
666-
<DirsToDeleteWithTrailingSeparator Remove="$(BuildObjDir)" />
667-
</ItemGroup>
668-
669-
<Message Text="DirSize After Building $(RepositoryName)" Importance="High" Condition="'$(BuildOS)' != 'windows' and '@(DirsToDeleteWithTrailingSeparator)' != ''" />
670-
<Exec Command="df -h $(RepoRoot)" Condition="'$(BuildOS)' != 'windows' and '@(DirsToDeleteWithTrailingSeparator)' != ''" />
671-
672-
<!-- Cleanup everything else. Ignore errors as out-of-band build servers don't reliably shut down, even with the build-server shutdown command.
673-
https://github.com/dotnet/source-build/issues/4175 tracks a long term fix. -->
674-
<RemoveDir Directories="@(DirsToDeleteWithTrailingSeparator)" ContinueOnError="true" />
678+
<Exec Command="$(RepoCleanCommand)" IgnoreExitCode="true" IgnoreStandardErrorWarningFormat="true" />
675679

676-
<Message Text="DirSize After CleanupRepo $(RepositoryName)" Importance="High" Condition="'$(BuildOS)' != 'windows'and '@(DirsToDeleteWithTrailingSeparator)' != ''" />
677-
<Exec Command="df -h $(RepoRoot)" Condition="'$(BuildOS)' != 'windows'and '@(DirsToDeleteWithTrailingSeparator)' != ''" />
680+
<Message Text="DirSize After CleanupRepo $(RepositoryName)" Importance="High" Condition="'$(BuildOS)' != 'windows'" />
681+
<Exec Command="df -h $(RepoRoot)" Condition="'$(BuildOS)' != 'windows'" />
678682

679683
<MakeDir Directories="$(BaseIntermediateOutputPath)" />
680684
<Touch Files="$(BaseIntermediateOutputPath)CleanupRepo.complete" AlwaysCreate="true">
@@ -720,7 +724,9 @@
720724
<ExtractedToolFiles Include="$(SourceBuiltSdksDir)%(_ToolPackage.Id)/**/*netcore*/*.dll" />
721725
</ItemGroup>
722726

723-
<Copy SourceFiles="@(ExtractedToolFiles)" DestinationFolder="$(SourceBuiltSdksDir)/" />
727+
<Copy SourceFiles="@(ExtractedToolFiles)"
728+
DestinationFolder="$(SourceBuiltSdksDir)/"
729+
UseHardlinksIfPossible="true" />
724730

725731
<MakeDir Directories="$(BaseIntermediateOutputPath)" />
726732
<Touch Files="$(BaseIntermediateOutputPath)ExtractToolPackage.complete" AlwaysCreate="true">

0 commit comments

Comments
 (0)