0

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>
Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Why don't you set the condition on your `CustomBuild`? `` – Troopers Oct 05 '16 at 15:30
  • @Troopers - We probably can for the ASM code; I'll have to look it up to see if its allowed. But we also have code that's cpu specific in `arm_cpu.cpp`, `x86_cpu.cpp`, etc. The CPP files don't have custom build rules. – jww Oct 05 '16 at 16:47
  • You could create a target CustomBuild that call special targets for arm, win32 et x64. Then add special Item in a ItemGroup in this targets then call the default Build – Troopers Oct 06 '16 at 06:54
  • Thanks @Troopers. The best I can tell [MSBuild does not support ARM](http://stackoverflow.com/q/39902108). I'm fairly sure the exercise above is a moot point. I'll leave the question open rather than deleting it in case someone has the insight to share for others. – jww Oct 06 '16 at 18:48
  • Seems this issue is based on issue of MSBuild does not support ARM. – starian chen-MSFT Oct 07 '16 at 06:24

0 Answers0