<div dir="ltr"><div>Hello,</div><div><br></div><div>I've run into a problem where rerunning CMake (for instance, automatically from Visual Studio via the ZERO_CHECK target) would sometimes switch to a different generator, like from 64-bit to 32-bit, completely messing up the build directory.</div><div><br></div><div>The reason is that CMake can drop information about the previously chosen generator. In such cases, when CMake is called again, it will try to detect the generator anew, and there is no guarantee that will choose the previous generator.</div><div>It's easily reproduced:</div><div><br></div><div>A dummy CMakeLists.txt:</div><div><br></div><div>    cmake_minimum_required(VERSION 3.3)</div><div>    project(problem)</div><div>    # message(FATAL_ERROR ouch)</div><div>    # undefined_function()</div><div>    # find_package(missing_package REQUIRED)</div><div><br></div><div>A call to</div><div>> cmake -G "Visual Studio 12 2013 Win64"<span class="" style="white-space:pre">           </span></div><div><br></div><div>will store this entry in CMakeCache.txt:<span class="" style="white-space:pre">  </span></div><div>CMAKE_GENERATOR:INTERNAL=Visual Studio 12 2013 Win64</div><div><br></div><div>So far, so good. Now uncomment any of the three commented lines in CMakeLists.txt and either run cmake again without explicitly specifying the generator or build the VS solution (which effectively does the same). The cmake run will fail and the CMakeCache.txt is re-written, but it will now lack the CMAKE_GENERATOR entry.</div><div><br></div><div>Now the build directory is doomed unless one explicitly specifies the Win64 generator again. To see why, fix the CMakeLists.txt by commenting out the bad line.</div><div>CMake will detect the generator anew and (depending on platform and installed IDEs, of course) might come up with "Visual Studio 12 2013". Note that detection will never use the Win64 version of that generator, so it will always guess wrong in this case. Furthermore, because there was no generator entry previously, CMake will not fail because of non-matching generators and happily create a mess of broken projects in the build directory.</div><div><br></div><div>I couldn't so far find a reasonable workaround. Why does CMake rewrite the CMakeCache in the case of a fatal error anyway? Can't it at least preserve the previous value of CMAKE_GENERATOR?</div><div><br></div><div>Thanks,</div><div>Jens</div></div>