gpt4 book ai didi

c++ - MsBuild 并行编译和构建依赖项

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:31 25 4
gpt4 key购买 nike

我正在研究一个包含很多项目的大型 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>

不起作用,在此设置中解决依赖关系不会构建依赖项目。

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

最佳答案

这是一个可能的解决方案:首先通过从解决方案文件中“解析”它们来获取所有项目的列表。如果您已经有了该列表,则不需要。然后为所有项目调用 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"/>

关于c++ - MsBuild 并行编译和构建依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32211298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com