Windows 10下CMake无法找到Visual Studio 2013工具链的问题排查

迫于生计,紧急支援了一个客户端项目。这个项目使用CMake进行工程管理,在Windows下,为了支持XP及一些特殊原因,需要先生成Visual Studio 2013工程,再进行编译。
按照README上说的,在开发机上安装完Visual Studio 2013和CMake,再运行自动化脚本,相关工程就会自动创建并编译,我就可以点个咖啡,享受C++程序员的福利时间。
然而现实是残酷的,咖啡还没下单,自动化脚本就报错了。
执行

1
cmake SOURCE_PATH -G "Visual Studio 12 2013"

时报错:
The CXX compiler identification is unknown
看着像是没找到Visual Studio 2013的工具链。

印象中之前也解决过类似的问题,删除CMakeCache.txt、升级CMake、重装Visual Studio、手动设置CMAKE_CXX_COMPILER。结果一轮操作下来,还是相同的报错:
The CXX compiler identification is unknown
一个上午过去了,连开发环境都没部署完,这就很尴尬了。难道要放重装系统的大招?

正好点的咖啡到了。喝了一杯咖啡,冷静了一些,终于决定先看一下CMake自己的报错信息。
打开CMakeError.log,发现里面有大量类似的报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Build started 11/30/2021 11:10:35 PM.
Project "SecretPath\CMakeFiles\3.21.1\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
PrepareForBuild:
Creating directory "Debug\".
Creating directory "Debug\CompilerIdC.tlog\".
InitializeBuildStatus:
Creating "Debug\CompilerIdC.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /nologo /W0 /WX- /Od /Oy- /D _USING_V110_SDK71_ /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TC /analyze- /errorReport:queue CMakeCCompilerId.c
CMakeCCompilerId.c
CMakeCCompilerId.c : fatal error C1001: An internal error has occurred in the compiler. [SecretPath\CMakeFiles\3.21.1\CompilerIdC\CompilerIdC.vcxproj]
(compiler file 'f:\dd\vctools\compiler\cxxfe\sl\p1\c\p0io.c', line 2807)
To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Done Building Project "SecretPath\CMakeFiles\3.21.1\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.

是因为无法编译这些测试文件,才导致CMake无法识别么?
拿着fatal error C1001f:\dd\vctools\compiler\cxxfe\sl\p1\c\p0io.c作为关键字搜了一波,还真搜到了相关的信息。
fatal error C1001: An internal error has occurred in the compiler. ‘f:\dd\vctools\compiler\cxxfe\sl\p1\c\p0io.c
在这个问题下,有人提到操作系统的locale设置会影响编译器编译p0io.c这个文件。关闭Windows 10区域设置中的Beta版: 使用Unicode UTF-8提供全球语言支持 / Beta: Use Unicode UTF-8 for worldwide language support就可以解决这个问题。(更详细的讨论)
碰巧我的开发机之前为了验证该设置对程序的影响,手动打开过。之后就再也没管过。关闭设置之后,再重新执行CMake,终于成功识别出了Visual Studio 2013的工具链。
总结一下,遇到问题先看日志,别急着套用之前的解决方案。CMake这种成熟的开源软件,给出的错误日志都很详细,直接对着日志排查/上网搜索关键字,往往比无脑重装软件更快。