1

Let's say I have this definition

<MSBuild
    Projects="$(MSBuildProjectFile)"
    Condition="'@(FilesToCompile)' != ''"
    Targets="buildcpp"
    Properties="CPPFILE=%(FilesToCompile.FullPath);OBJFILE=$(ObjectFolder)\%(FilesToCompile.Filename).doj;IncludeDirs=$(IncludeDirs)"
/>

This target is executed multiple times due to task batching, once for each file in FilesToCompile. Importantly, each invocation is completely independent, making it perfect for parallelization.

QUESTION

How do I enable "parallel-mode" for task batching?

NOT A DUPLICATE

I want to invoke the same target multiple times with different property values and to do that, I need the batching to be done in parallel.

The linked question does not do batching and it uses different projects.

Bob
  • 4,576
  • 7
  • 39
  • 107
  • 1
    Possible duplicate of [How can I build multiple configurations in parallel?](https://stackoverflow.com/questions/12984882/how-can-i-build-multiple-configurations-in-parallel) – stijn Nov 07 '17 at 08:35
  • I don't see how this is different from the linked duplicate: have you tried the sample code of the answer? It builds the same project multiple times with different properties, in parallel. Exactly what you are asking? – stijn Nov 07 '17 at 15:18
  • @stijn sample code doesn't iterate over the ItemGroup `FilesToCompile.FullPath`. With the linked solution, I'd have to manually "unwind" the complete item group. – Bob Nov 07 '17 at 15:21
  • That is a completely different problem: it's about how to create an ItemGroup which contains the project several times, each with a different value for the Properties metadata. Which you don't have to do manually, you just declare an item using batching. So you don't *need* parallel batching, which would in this case be trying to push a circle through a square hole: just use the canonical MSBuild way, using well-known and built-in functionality. – stijn Nov 07 '17 at 15:32
  • @stijn ok. How do I do it? – Bob Nov 07 '17 at 15:33
  • Just put what you have for `Properties=...` as metadata in an item. From the top of my head something like ` CPPFILE=%(FilesToCompile.FullPath);etc... ` then `` – stijn Nov 07 '17 at 15:37

2 Answers2

0

MSBuild supports parallelism at the project level and in addition for C/C++ projects at CL compiler level. To have MSBuild start using parallelism in your case it is necessary to provide multiple projects and pass them to MSBuild as either (i) a dependency of single project or (ii) solution with defined build graph. In addition it is required to pass /m or /maxcpucount on a command line to MSBuild and it is advised to use /nodereuse:false or /nr:false.

In principle you have to create/generate multiple projects each for one invocation of your target.

Jacek Blaszczynski
  • 3,183
  • 14
  • 25
  • See https://stackoverflow.com/questions/12984882/how-can-i-build-multiple-configurations-in-parallel: no need to create multiple projects – stijn Nov 07 '17 at 08:36
0

How do I enable "parallel-mode" for this task?

To run builds in parallel, we could use /maxcpucount switch at a command prompt or BuildInParallel task parameter on an MSBuild task. So you should use the BuildInParallel task parameter for your task.

The default value of BuildInParallel task parameter is false, we need set it to true:

<PropertyGroup>  
    <BuildInParallel Condition="'$(BuildInParallel)' == ''">true</BuildInParallel>  
</PropertyGroup>

<MSBuild
    Projects="$(MSBuildProjectFile)"
    Condition="'@(FilesToCompile)' != ''"
    Targets="buildcpp"
    BuildInParallel="$(BuildInParallel)" 
    Properties="CPPFILE=%(FilesToCompile.FullPath);OBJFILE=$(ObjectFolder)\%(FilesToCompile.Filename).doj;IncludeDirs=$(IncludeDirs)"
/>

See Building Multiple Projects in Parallel with MSBuild and new parallel task in the MSBuild Extension Pack for more detail info.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    This doesn't answer the question. The OP wants to build the same project multiple times with different properties, and do that in parallel. – stijn Nov 07 '17 at 08:35
  • I agree with @stijn. I updated the question to explain why it's different than the **potential duplicate** – Bob Nov 07 '17 at 15:15