MSBuild has an ExcludedFromBuild
which excludes a file based on condition. I want the opposite pattern - I want to include a file based on a condition.
It appears I cannot add a condition to an ItemGroup
, which is the node source files are included. Also see Why the Condition
attribute doesn't work for the ItemGroup
element? So something like this does not work:
<ItemGroup Condition="'$(Platform)'=='x64'">
<ClCompile Include="x64.cpp" />
</ItemGroup>
How do I conditionally include files in build?
The reason for the change is we are adding better support for ARM. We think the project files can be re-purposed by adding an ARM section like X86 and X64:
<!-- X86 Configurations -->
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'" Label="X86 Configuration">
<ClCompile>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<TargetMachine>MachineX86</TargetMachine>
</Link>
</ItemDefinitionGroup>
<!-- X64 Configurations -->
<ItemDefinitionGroup Condition="'$(Platform)'=='x64'" Label="X64 Configuration">
<ClCompile>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<TargetMachine>MachineX64</TargetMachine>
</Link>
</ItemDefinitionGroup>
However, we have this unscalable wart code, and I feel it should be re-written to include on x64 platforms (whitelist), rather than exclude on non-x64 platforms (blacklist). Its obviously going to break on ARM, and it will break again if Microsoft supports MIPS for IoT.
<CustomBuild Include="x64masm.asm">
<Message>Building and Assembling x64masm.asm</Message>
<ExcludedFromBuild Condition="'$(Platform)'=='Win32'">true</ExcludedFromBuild>
<Command>ml64.exe /c /nologo /D_M_X64 /W3 /Fo"$(IntDir)x64masm.obj" /Zi "%(FullPath)"</Command>
<Outputs>$(IntDir)x64masm.obj;%(Outputs)</Outputs>
</CustomBuild>