Qt Moc'ing multiple files in parallel under msbuild

核能气质少年 提交于 2019-12-05 10:05:26

I am not very familiar with Visual Studio projects ... so don't know if the "CustomBuild" ItemGroup name and its metadata names bear significance. If they do, you might need to

  1. Modify the ItemGroup name to something else
  2. Identify the msbuild targets file which handles that and fix it for parallel processing

Here is a standalone msbuild file that can execute things in parallel.

Notes:

  1. The ping -n 30 -w 1000 127.0.0.2>nul command has been added so that I could launch process explorer and find enough time to count how many instances of ping.exe were spawned.
  2. I was trying to ping 127.0.0.2 because I wanted ping not to finish earlier.
  3. There is nothing practically useful happening in the file ... only echoing properties as a proof of concept.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="RunMe">
    <ItemGroup>
        <CustomBuild Include="a_class4.h">
            <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>
            <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Moc%27ing %(Identity)...</Message>
            <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</Outputs>
            <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(QTDIR)\bin\moc.exe"  "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp"  -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_CORE_LIB -DQT_GUI_LIB "-I." "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I$(QTDIR)\include\qtmain" "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I." "-I." "-I." "-I."</Command>
        </CustomBuild>
        <CustomBuild Include="a_class3.h">
            <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>
            <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Moc%27ing %(Identity)...</Message>
            <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</Outputs>
            <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(QTDIR)\bin\moc.exe"  "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp"  -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_CORE_LIB -DQT_GUI_LIB "-I." "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I$(QTDIR)\include\qtmain" "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I." "-I." "-I." "-I."</Command>
        </CustomBuild>
    </ItemGroup>
    <Target Name="RunMe">
        <Message Text="First populate an ItemGroup such that we can use it for parallel processing"/>
        <ItemGroup>
            <InputForParallelInvoke Include="$(MSBuildThisFileFullPath)">
                <Properties>InputFile=%(CustomBuild.Identity);
                        AdditionalInputs=%(CustomBuild.AdditionalInputs);
                        Message=%(CustomBuild.Message);
                        Outputs=%(CustomBuild.Outputs);
                        Command=%(CustomBuild.Command)</Properties>

            </InputForParallelInvoke>
        </ItemGroup>
        <MSBuild Projects="@(InputForParallelInvoke)" Targets="InvokeInParallel" BuildInParallel="True" />
    </Target>
    <Target Name="InvokeInParallel">
        <Exec Command="ping -n 30 -w 1000 127.0.0.2>nul &amp; echo Properties=$(InputFile) ##### $(AdditionalInputs) ##### $(Message) ##### $(Outputs) ##### $(Command)"/>
    </Target>
</Project>

When I invoke it on via msbuild thus msbuild /m \path\to\file /p:Configuration=Debug;Platform=Win32 I see 2 instances of ping.exe.

Hope this helps solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!