MsBuild并行编译和构建依赖项

时间:2015-08-25 18:19:47

标签: c++ msbuild

我正在开发一个包含大量项目的大型C ++解决方案。

其中一些是构建瓶颈,其中dll依赖于另一个需要永久构建的东西。

我有很多CPU需要构建,但我不能让MSBuild并行编译(不链接)所有内容,只在链接时使用依赖项。

我基本上希望每个项目都有:

# build objects
msbuild /t:BuildCompile project.vcxproj

# only now build/wait for dependencies
msbuild /t:ResolveReferences;BuildLink project.vcxproj

我希望以上工作作为单个构建的一部分(级联到依赖项目)。

我一直试图搞砸MSBuild目标构建订单:

<PropertyGroup>
  <BuildSteps>
    SetBuildDefaultEnvironmentVariables;
    SetUserMacroEnvironmentVariables;
    PrepareForBuild;
    InitializeBuildStatus;
    BuildGenerateSources;
    BuildCompile;

    ResolveReferences;

    BuildLink;
  </BuildSteps>
</PropertyGroup>

不起作用,此安装程序中的Resolve Dependencies不构建依赖项目。

有什么想法吗?只有链接器实际上依赖于引用的项目,objs不会。

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案:首先通过从解决方案文件中“解析”它们来获取所有项目的列表。如果您已经拥有该列表,则不需要。然后为所有项目调用msbuild两次,一次使用BuildCompile目标,然后使用Build目标。我特意选择了Build目标(因为我已经完成了将会跳过编译)因为我不确定你所提出的只调用ResolveReferences和Link目标的解决方案会在所有情况下成功构建,例如它可能会跳过资源编译,跳过自定义构建步骤等。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <AllTargets Include="BuildCompile;Build" />
  </ItemGroup>
  <Target Name="Build">
    <ReadLinesFromFile File="mysolution.sln">
      <Output TaskParameter="Lines" ItemName="Solution" />
    </ReadLinesFromFile>

    <ItemGroup>
     <AllProjects Include="$([System.Text.RegularExpressions.Regex]::Match('%(Solution.Identity)', ', &quot;(.*\.vcxproj)&quot;').Groups[ 1 ].Value)"/>
    </ItemGroup>

    <MSBuild BuildInParallel="true" Projects="@(AllProjects)"
             Properties="Configuration=$(Configuration);Platform=$(Platform)"
             Targets="%(AllTargets.Identity)"/>
  </Target>
</Project>

调用

msbuild mybuild.proj /p:Configuration=Debug;Platform=Win32

我很想知道这是否会改善您的构建时间。

编辑,因为您看到的是完全重建的外观,也许BuildCompile目标只有在BuildSteps中的其他目标运行时才能正常工作。您可以尝试明确地拆分构建:

<MSBuild BuildInParallel="true" Projects="@(AllProjects)"
         Properties="Configuration=$(Configuration);Platform=$(Platform)"
         Targets="SetBuildDefaultEnvironmentVariables;
                  SetUserMacroEnvironmentVariables;
                  PrepareForBuild;
                  InitializeBuildStatus;
                  BuildGenerateSources;
                  BuildCompile;"/>

<MSBuild BuildInParallel="true" Projects="@(AllProjects)"
         Properties="Configuration=$(Configuration);Platform=$(Platform)"
         Targets="Build"/>