Need help with CMAKE_MSVC_RUNTIME_LIBRARY variable

I tried building static libjxl with MSVC toolchain and clang with GNU-Like command by specifying -DCMAKE_CXX_COMPILER=clang++ in release mode, but it failed.

I bisected the code and the issue started after this pull request. Which shouldn’t be possible since this code

set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$<NOT:$<CONFIG:Debug>>:MultiThreaded>" CACHE STRING "")

should produce the correct string which is MultiThreaded. I tested this with cl and clang-cl, all of them worked, but not clang++. I made sure to not build it in debug mode with -DCMAKE_BUILD_TYPE=Release, but to no avail. I tried inspecting the produced value from generator by putting it into a dummy variable, and it correctly produces “MultiThreaded”.

  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
  set(CMAKE_DUMMY_VAR "$<$<NOT:$<CONFIG:Debug>>:MultiThreaded>" CACHE STRING "")
  file(GENERATE OUTPUT runtime.txt CONTENT ${CMAKE_DUMMY_VAR})

For context, this is the linked issues.
Can’t set CMAKE_MSVC_RUNTIME_LIBRARY with generator when paired with Clang GNU driver (#26330) · Issues · CMake / CMake · GitLab (kitware.com)

This is the log from configuration

    kind: "try_compile-v1"
    backtrace:
      - "C:/Users/Alifian/scoop/apps/cmake/3.30.3/share/cmake-3.30/Modules/Internal/CheckSourceCompiles.cmake:101 (try_compile)"
      - "C:/Users/Alifian/scoop/apps/cmake/3.30.3/share/cmake-3.30/Modules/CheckCXXSourceCompiles.cmake:52 (cmake_check_source_compiles)"
      - "cmake/FindAtomics.cmake:32 (check_cxx_source_compiles)"
      - "C:/Users/Alifian/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake:859 (_find_package)"
      - "CMakeLists.txt:262 (find_package)"
    checks:
      - "Performing Test ATOMICS_LOCK_FREE_INSTRUCTIONS"
    directories:
      source: "D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-4jbfvf"
      binary: "D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-4jbfvf"
    cmakeVariables:
      CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS: "C:/Program Files/LLVM/bin/clang-scan-deps.exe"
      CMAKE_CXX_FLAGS: "-flto=full -march=native"
      CMAKE_CXX_FLAGS_DEBUG: "-O0"
      CMAKE_CXX_LINK_NO_PIE_SUPPORTED: "NO"
      CMAKE_EXE_LINKER_FLAGS: " -static"
      CMAKE_MODULE_PATH: "D:/a/libjxl/cmake"
      CMAKE_MSVC_RUNTIME_LIBRARY: "$<$<NOT:$<CONFIG:Debug>>:MultiThreaded>"
      VCPKG_INSTALLED_DIR: "C:/Users/Alifian/scoop/apps/vcpkg/current/installed"
      VCPKG_PREFER_SYSTEM_LIBS: "OFF"
      VCPKG_TARGET_TRIPLET: "x64-win-llvm-lto-static-rel"
      Z_VCPKG_ROOT_DIR: "C:/Users/Alifian/scoop/apps/vcpkg/current"
    buildResult:
      variable: "ATOMICS_LOCK_FREE_INSTRUCTIONS"
      cached: true
      stdout: |
        Change Dir: 'D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-4jbfvf'
        
        Run Build Command(s): C:/PROGRA~1/MICROS~2/2022/COMMUN~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe -v cmTC_dd925
        [1/2] C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -DATOMICS_LOCK_FREE_INSTRUCTIONS  -flto=full -march=native  -O0 -std=c++17 -g -Xclang -gcodeview -MD -MT CMakeFiles/cmTC_dd925.dir/src.cxx.obj -MF CMakeFiles\\cmTC_dd925.dir\\src.cxx.obj.d -o CMakeFiles/cmTC_dd925.dir/src.cxx.obj -c D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-4jbfvf/src.cxx
        [2/2] C:\\WINDOWS\\system32\\cmd.exe /C "cd . && C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -nostartfiles -nostdlib -flto=full -march=native  -O0 -g -Xclang -gcodeview -static   -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_dd925.dir/src.cxx.obj -o cmTC_dd925.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_dd925.lib -Xlinker /pdb:cmTC_dd925.pdb -Xlinker /version:0.0   -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
        FAILED: cmTC_dd925.exe 
        C:\\WINDOWS\\system32\\cmd.exe /C "cd . && C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -nostartfiles -nostdlib -flto=full -march=native  -O0 -g -Xclang -gcodeview -static   -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_dd925.dir/src.cxx.obj -o cmTC_dd925.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_dd925.lib -Xlinker /pdb:cmTC_dd925.pdb -Xlinker /version:0.0   -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
        lld-link: error: <root>: undefined symbol: mainCRTStartup
        clang++: error: linker command failed with exit code 1 (use -v to see invocation)
        ninja: build stopped: subcommand failed.
        
      exitCode: 1
  -
    kind: "try_compile-v1"
    backtrace:
      - "C:/Users/Alifian/scoop/apps/cmake/3.30.3/share/cmake-3.30/Modules/Internal/CheckSourceCompiles.cmake:101 (try_compile)"
      - "C:/Users/Alifian/scoop/apps/cmake/3.30.3/share/cmake-3.30/Modules/CheckCXXSourceCompiles.cmake:52 (cmake_check_source_compiles)"
      - "cmake/FindAtomics.cmake:39 (check_cxx_source_compiles)"
      - "C:/Users/Alifian/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake:859 (_find_package)"
      - "CMakeLists.txt:262 (find_package)"
    checks:
      - "Performing Test ATOMICS_IN_LIBRARY"
    directories:
      source: "D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-042yl2"
      binary: "D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-042yl2"
    cmakeVariables:
      CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS: "C:/Program Files/LLVM/bin/clang-scan-deps.exe"
      CMAKE_CXX_FLAGS: "-flto=full -march=native"
      CMAKE_CXX_FLAGS_DEBUG: "-O0"
      CMAKE_CXX_LINK_NO_PIE_SUPPORTED: "NO"
      CMAKE_EXE_LINKER_FLAGS: " -static"
      CMAKE_MODULE_PATH: "D:/a/libjxl/cmake"
      CMAKE_MSVC_RUNTIME_LIBRARY: "$<$<NOT:$<CONFIG:Debug>>:MultiThreaded>"
      VCPKG_INSTALLED_DIR: "C:/Users/Alifian/scoop/apps/vcpkg/current/installed"
      VCPKG_PREFER_SYSTEM_LIBS: "OFF"
      VCPKG_TARGET_TRIPLET: "x64-win-llvm-lto-static-rel"
      Z_VCPKG_ROOT_DIR: "C:/Users/Alifian/scoop/apps/vcpkg/current"
    buildResult:
      variable: "ATOMICS_IN_LIBRARY"
      cached: true
      stdout: |
        Change Dir: 'D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-042yl2'
        
        Run Build Command(s): C:/PROGRA~1/MICROS~2/2022/COMMUN~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe -v cmTC_b8619
        [1/2] C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -DATOMICS_IN_LIBRARY  -flto=full -march=native  -O0 -std=c++17 -g -Xclang -gcodeview -MD -MT CMakeFiles/cmTC_b8619.dir/src.cxx.obj -MF CMakeFiles\\cmTC_b8619.dir\\src.cxx.obj.d -o CMakeFiles/cmTC_b8619.dir/src.cxx.obj -c D:/a/libjxl/build/CMakeFiles/CMakeScratch/TryCompile-042yl2/src.cxx
        [2/2] C:\\WINDOWS\\system32\\cmd.exe /C "cd . && C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -nostartfiles -nostdlib -flto=full -march=native  -O0 -g -Xclang -gcodeview -static   -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_b8619.dir/src.cxx.obj -o cmTC_b8619.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_b8619.lib -Xlinker /pdb:cmTC_b8619.pdb -Xlinker /version:0.0   -latomic  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
        FAILED: cmTC_b8619.exe 
        C:\\WINDOWS\\system32\\cmd.exe /C "cd . && C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -nostartfiles -nostdlib -flto=full -march=native  -O0 -g -Xclang -gcodeview -static   -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_b8619.dir/src.cxx.obj -o cmTC_b8619.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_b8619.lib -Xlinker /pdb:cmTC_b8619.pdb -Xlinker /version:0.0   -latomic  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
        lld-link: error: could not open 'atomic.lib': no such file or directory
        clang++: error: linker command failed with exit code 1 (use -v to see invocation)
        ninja: build stopped: subcommand failed.
        
      exitCode: 1

Notice how CMAKE_MSVC_RUNTIME_LIBRARY is still in its generator form instead of the correct input. And this only happens if I specify clang GNU-like driver, cl and clang-cl are working fine.