Ускорьте сборку с помощью fsutil hardlink create вместо задач медленного копирования - PullRequest
7 голосов
/ 23 мая 2011

Я пытаюсь ускорить нашу сборку (csharp, msbuild, .net 3.5). Замените копию на fsutil hardlink create.

Раньше мне почти удавалось запустить скрипт над файлами sln и сделать ссылки на dll private = false, а затем с помощью события после сборки создать жесткие ссылки. Проблема в том, что переходные зависимости не включены. Итак, я думаю, что мне нужно сослаться на задачу ResolveAssemblyReference в msbuild, чтобы получить переходные зависимости, которые мне нужны для жесткой ссылки.

Есть идеи?

Этот человек попробовал то же самое, но не опубликовал окончательное решение.

Чтобы быть ясным: я хочу сохранить отдельные каталоги bin, а не копировать файл из одного в другой, чтобы создать жесткую ссылку из источника (ссылки или зависимости) на место назначения (bin текущего проекта) ). Это намного быстрее и дает примерно такой же эффект, как копирование.

Ответы [ 2 ]

6 голосов
/ 08 июня 2011

Это поддерживается в VS 2010. Но не в 2008 г. См. Параметр UseHardLinksIfPossible для копирования в _CopyFilesMarkedCopyLocal.

См. Также http://social.msdn.microsoft.com/Forums/en/tfsbuild/thread/9382a3d8-4632-4826-ad15-d5e845080981, http://msdn.microsoft.com/en-us/library/ms171466(v=VS.90).aspx для контекста.

Переопределить цель _CopyFilesMarkedCopyLocal.Мы добавляем что-то вроде этого в файлы csproj внизу: <Import Project="..\..\..\..\..\\CommonBuild\TW.Override.Microsoft.Common.targets" /> (Это автоматически добавляется в файл с задачей nant при каждой полной сборке nant, так что каждый проект имеет преимущества).

Наш новый файл целей:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <UsingTask TaskName="CopyWithHardlinkOption" AssemblyFile="..\lib\TWBuildOptimization\TW.Hardlinker.dll" />
  <!--
    ============================================================
                                        _CopyFilesMarkedCopyLocal
    Overridden in order to allow hardlinking with our custom Copy Task.                                         

    Hardlinking is a major performance improvement. Sometimes 50% of the time compared to copying.

    Copy references that are marked as "CopyLocal" and their dependencies, including .pdbs, .xmls and satellites.
    ============================================================
  -->
    <Target
        Name="_CopyFilesMarkedCopyLocal">
        <CopyWithHardlinkOption
            SourceFiles="@(ReferenceCopyLocalPaths)"
            DestinationFiles="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')"
            SkipUnchangedFiles="true"
              UseHardlinksIfPossible="true"
            OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)">
            <Output TaskParameter="DestinationFiles" ItemName="FileWritesShareable"/>
        </CopyWithHardlinkOption>
    </Target>
</Project>

См. Опцию msbuild 4 Copy UseHardlinksIfPossible.Я перенес это на 3,5 через декомпиляцию и переопределение.Соответствующая логика в CopyFileWithLogging была:

      // The port from 4.0's task that allows hardlinking
        bool hardlinkSucceeded = false;
        if (UseHardlinksIfPossible)
        {
            if (File.Exists(destinationFile))
            {
                FileUtilities.DeleteNoThrow(destinationFile);
            }
            if (!TwNativeMethods.CreateHardLink(destinationFile, sourceFile, IntPtr.Zero))
            {
                var win32Exception = new Win32Exception(Marshal.GetLastWin32Error());
                Log.LogMessage(MessageImportance.High, "Hardlinking had a problem {0}, will retry copying. {1}", new object[] {win32Exception.Message, win32Exception});
            }
            hardlinkSucceeded = true;
        }
        if (!hardlinkSucceeded)
        {
            Log.LogMessageFromResources(MessageImportance.Normal, "Copy.FileComment", new object[] { sourceFile, destinationFile });
            Log.LogMessageFromResources(MessageImportance.Low, "Shared.ExecCommand", new object[0]);
            Log.LogCommandLine(MessageImportance.Low, "copy /y \"" + sourceFile + "\" \"" + destinationFile + "\"");
            File.Copy(sourceFile, destinationFile, true);
        }
        // end port

Также пришлось добавить следующие:

// decompiled from 4.0
internal static class TwNativeMethods
{
    [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    internal static extern bool CreateHardLink(string newFileName, string exitingFileName, IntPtr securityAttributes);
}

// decompiled from 4.0
internal static class FileUtilities
{
    internal static void DeleteNoThrow(string path)
    {
        try
        {
            File.Delete(path);
        }
        catch (Exception exception)
        {
            if (ExceptionHandling.NotExpectedException(exception))
            {
                throw;
            }
        }
    }
}

// decompiled from 4.0
internal static class ExceptionHandling
{
    // Methods
    internal static bool IsCriticalException(Exception e)
    {
        return (((e is StackOverflowException) || (e is OutOfMemoryException)) || ((e is ExecutionEngineException) || (e is AccessViolationException)));
    }

    internal static bool NotExpectedException(Exception e)
    {
        return (((!(e is UnauthorizedAccessException) && !(e is ArgumentNullException)) && (!(e is PathTooLongException) && !(e is DirectoryNotFoundException))) && ((!(e is NotSupportedException) && !(e is ArgumentException)) && (!(e is SecurityException) && !(e is IOException))));
    }

    internal static bool NotExpectedReflectionException(Exception e)
    {
        return ((((!(e is TypeLoadException) && !(e is MethodAccessException)) && (!(e is MissingMethodException) && !(e is MemberAccessException))) && ((!(e is BadImageFormatException) && !(e is ReflectionTypeLoadException)) && (!(e is CustomAttributeFormatException) && !(e is TargetParameterCountException)))) && (((!(e is InvalidCastException) && !(e is AmbiguousMatchException)) && (!(e is InvalidFilterCriteriaException) && !(e is TargetException))) && (!(e is MissingFieldException) && NotExpectedException(e))));
    }
}
0 голосов
/ 23 мая 2011

Вы пытались определить свою собственную цель, которая запускается после цели ResolveAssemblyReferences? После запуска ResolveAssemblyReferences вы сможете использовать элемент @ (ReferencePath) для управления созданием ссылки. Рассмотрите возможность реализации AfterResolveReferences, которая в Microsoft.Common.targets является просто пустым заполнителем для переопределения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...