Can't set property via .props file

Bogdan Glinskiy 1 Reputation point
2022-02-10T13:38:57.737+00:00

I'm trying to change project's property via .props file. I created 2 files:

Directory.build.props

<Project>
<PropertyGroup Label="Globals">
<ForceImportBeforeCppProps>$(MsbuildThisFileDirectory)\MyProps.props</ForceImportBeforeCppProps>
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
</PropertyGroup>
</Project>

And

MyProps.props

<Project>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<PlatformToolset>v142</PlatformToolset>
<PreferredToolArchitecture>x86</PreferredToolArchitecture>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<PlatformToolset>v142</PlatformToolset>
<PreferredToolArchitecture>x86</PreferredToolArchitecture>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<PlatformToolset>v142</PlatformToolset>
<PreferredToolArchitecture>x86</PreferredToolArchitecture>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<PlatformToolset>v142</PlatformToolset>
<PreferredToolArchitecture>x86</PreferredToolArchitecture>
</PropertyGroup>
</Project>

Both properties WindowsTargetPlatformVersion and PlatformToolset are successfully applied, but not the PreferredToolArchitecture. Could you please tell me, where I'm wrong? How to set it properly? I'm, using Visual Studio 2019

I also found, that this works if I delete PreferredToolArchitecture property from the .vcxproj file. Why it can't override it?

Developer technologies | Visual Studio | Other
Community Center | Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2022-02-10T16:00:15.85+00:00

    Firstly I'd say that since you're using the same value in all cases you don't need it to be in a conditional property group. Just set it directly in directory.build.props or in a property group without a condition.

    <PropertyGroup>
       <PreferredToolArchitecture>x86</PreferredToolArchitecture>
    </PropertyGroup>
    

    Secondly, the .props stuff (and directory.build.props for that matter) run before your project's properties are applied. Project properties override these values. So if you're also setting this in your project file then the project file wins every time. If you need to force all projects to use a property irrelevant of what the project file actually says then the correct solution is to use a conditional check in the project file that says to use value X when property Y is not already set.

    Alternatively you can overwrite the property values after the project properties have been loaded. But to do that you have to use a directory.build.targets file instead. You'll need to hook into the build process after the project file has been loaded but before compilation. I'm not 100% sure on C++ projects but BeforeBuild is a good starting point. Within the Target set the property to the value you want. This will overwrite any previous assignment.

    <Project>
       <Target Name="UpdateArchitecture" BeforeTargets="BeforeBuild"> 
          <PropertyGroup>
             <PreferredToolArchitecture>x86</PreferredToolArchitecture>
          </PropertyGroup>
       </Target>
    </Project>
    

    If you need the actual value to be configurable then create a custom property in your directory.build.props (or equivalent) file and then use that in the .targets file to set the actual value that MSBuild will use.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.