CMAKE_COLOR_MAKEFILE=OFF breaking clang++ compilation on Windows/Msys setup

When trying to figure out another issue I disabled colored Makefiles for a project I’m building with CMake on Windows. But it looks like this breaks the compilation:

[build] clang++: error: unknown argument: '-fno-ansi-escape-codes'

I guess the offending code is in https://gitlab.kitware.com/cmake/cmake/-/blob/v3.28.2/Modules/Compiler/Clang.cmake#L123

    # '-fcolor-diagnostics' introduced since Clang 2.6
    if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 2.6)
      # -fansi-escape-codes mentioned at https://releases.llvm.org/3.7.0/tools/clang/docs/UsersManual.html
      if (CMAKE_HOST_WIN32 AND CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 3.7)
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS -fansi-escape-codes -fcolor-diagnostics)
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -fno-ansi-escape-codes  -fno-color-diagnostics)
      else()
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS -fcolor-diagnostics)
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -fno-color-diagnostics)
      endif()
    endif()

From the mentioned LLVM documentation it looks like there is no -fno-ansi-escape-codes, only -fansi-escape-codes:

-fansi-escape-codes
Controls whether ANSI escape codes are used instead of the Windows Console API to output colored diagnostics. This option is only used on Windows and defaults to off.

So I would consider this a bug introduced in CMake 3.8.2 and to fix it one needs to remove -fno-ansi-escape-codes from the Windows case.

I changed it in my CMake installation and this seems to fix my color issue:

    # '-fcolor-diagnostics' introduced since Clang 2.6
    if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 2.6)
      # -fansi-escape-codes mentioned at https://releases.llvm.org/3.7.0/tools/clang/docs/UsersManual.html
      if (CMAKE_HOST_WIN32 AND CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 3.7)
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS -fansi-escape-codes -fcolor-diagnostics)
-       set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -fno-ansi-escape-codes  -fno-color-diagnostics)
+       set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -fno-color-diagnostics)
      else()
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS -fcolor-diagnostics)
        set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -fno-color-diagnostics)
      endif()
    endif()

My setup

  • Windows 11
  • MSYS2 with UCRT64 environment
  • CMake v3.28.2 from MSYS2 UCRT64
  • clang v17.0.6, Target: x86_64-w64-windows-gnu from MSYS2 UCRT64

I’ve opened CMake Issue 26036 for this, thanks.

1 Like