visual-studiovisual-c++buildmsbuildvisual-studio-project

Visual Studio project is not rebuilt automatically if it's settings depend on the environment variable which is changed


I have a solution in Visual Studio 16.8.3 (Enterprise 2019). Projects of the solution have settings that depend on the value of the environment variable OPTIMIZE. Something like the following (other settings are omitted):

<ItemDefinitionGroup Condition="'$(OPTIMIZE)' == 'TRUE'">
  <ClCompile>
    <WholeProgramOptimization>true</WholeProgramOptimization>
  </ClCompile>
</ItemDefinitionGroup>
...
<ItemDefinitionGroup Condition="'$(OPTIMIZE)' != 'TRUE'">
  <ClCompile>
    <WholeProgramOptimization>false</WholeProgramOptimization>
  </ClCompile>
</ItemDefinitionGroup> 

I set OPTMIZE=TRUE and build the solution. Then I close VS, set OPTIMIZE=FALSE, open VS. Now I'm expecting that the whole solution will be rebuilt since every cpp's file compilation settings changed. But when I press Build Solution, nothing happens. When I right-click the project in the Solution Explorer and press Build, nothing happens again.

But, when I select any cpp file in any project and compile it, then the project somehow understands that the settings for all its cpp files changed. Now if I right-click the project in the Solution Explorer, and press Build, the whole project is rebuilt.

I want the project to understand that the settings changed when I change the value of the environment variable. How can I achieve this behavior?

P.S. Of course, I can use Rebuild Solution if I know the settings changed. But I don't want to keep in mind if the value of the OPTIMIZE variable changed since the last build. I would like just to press Build Solution and have a small incremental build if there were no changes and a complete rebuild if the settings changed.


Solution

  • VS IDE cannot detect the small change in my side and I faced the same behavior as you described. And in my thought, msbuild will detect the changes or modifications of your resource files compared with the output files and then if is newer of output files, it will build.

    So since your function does not add any new info to vcxproj file, just some logical processing, not add any more node in vcxproj file.

    However, I found that msbuild command line could realize what you need.

    So I wonder if up-to-date of VS IDE has some problems which could not detect the changed setting.

    And is also have the up-to-date mechanism:

    enter image description here

    When you change the OPTIMIZE env variable, in my side, I changed it from FALSE to TRUE, save it, restart Developer Command Prompt for VS, and then type: msbuild xxx\xxx.vcxproj -t:build and will find it build your project based on the changed property setting.

    enter image description here

    GL is WholeProgramOptimization to true.

    And the new output files are generated at the time of the build time which means it does a build.

    Besides, you could also create a bat file of the build process from msbuild command line. Once you should double-click on the bat, it will executes the build.

    Update

    VS IDE cannot get that so far. I am sorry to tell that. And I have reported the issue to our DC Team and you could check it, vote it and add any comments if I did not describe the issue in detail. And it will get more Microsoft's attention.

    And you have to use msbuild command line so far to get what you want.