microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.45k stars 443 forks source link

Clang-cl not working #3502

Open Wolfleader101 opened 8 months ago

Wolfleader101 commented 8 months ago

Brief Issue Summary

Same issue as #2798

image

[cmake] CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message):
[cmake]   The C compiler
[cmake] 
[cmake]     "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe"
[cmake] 
[cmake]   is not able to compile a simple test program.
[cmake] 
[cmake]   It fails with the following output:

This is only an issue with clang-cl , clang and clang++ (GNU-Like clang) work with no issues, however i need clang-cl for windows specific libs.

My CMakePresets

 "configurePresets": [
    {
      "name": "windows-clang-base",
      "hidden": true,
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/out/build/${presetName}",
      "installDir": "${sourceDir}/out/install/${presetName}",
      "cacheVariables": {
        "CMAKE_C_COMPILER": "clang-cl.exe",
        "CMAKE_CXX_COMPILER": "clang-cl.exe"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    },
     {
      "name": "clang-cl-x64-debug",
      "displayName": "Windows Clang x64 Debug",
      "inherits": "windows-clang-base",
      "architecture": {
        "value": "x64",
        "strategy": "external"
      },
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
      }
    },

CMake Tools Diagnostics

{
  "os": "win32",
  "vscodeVersion": "1.85.1",
  "cmtVersion": "1.16.32",
  "configurations": [
    {
      "folder": "c:\\code\\personal\\cpp\\FenrirEngine",
      "cmakeVersion": "3.27.2",
      "configured": true,
      "generator": "Ninja",
      "usesPresets": true,
      "compilers": {}
    }
  ],
  "cpptoolsIntegration": {
    "isReady": true,
    "hasCodeModel": false,
    "activeBuildType": "",
    "buildTypesSeen": [],
    "requests": [],
    "responses": [],
    "partialMatches": [],
    "targetCount": 0,
    "executablesCount": 0,
    "librariesCount": 0,
    "targets": []
  },
  "settings": [
    {
      "communicationMode": "automatic",
      "useCMakePresets": "auto",
      "configureOnOpen": true
    }
  ]
}

Debug Log

Exception has occurred: FATAL_ERROR
CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'C:/code/personal/cpp/FenrirEngine/out/build/clang-x64-debug/CMakeFiles/CMakeScratch/TryCompile-kd4vvn'

    Run Build Command(s): C:/PROGRA~1/MICROS~1/2022/COMMUN~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe -v cmTC_7f259
    [1/2] C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\Llvm\x64\bin\clang-cl.exe  /nologo   /DWIN32 /D_WINDOWS  /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmTC_7f259.dir\testCCompiler.c.obj /FdCMakeFiles\cmTC_7f259.dir\ -c -- C:\code\personal\cpp\FenrirEngine\out\build\clang-x64-debug\CMakeFiles\CMakeScratch\TryCompile-kd4vvn\testCCompiler.c
    [2/2] cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_7f259.dir --rc=C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\Llvm\x64\bin\llvm-rc.exe --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\Llvm\x64\bin\lld-link.exe /nologo CMakeFiles\cmTC_7f259.dir\testCCompiler.c.obj  /out:cmTC_7f259.exe /implib:cmTC_7f259.lib /pdb:cmTC_7f259.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    FAILED: cmTC_7f259.exe 
    cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_7f259.dir --rc=C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\Llvm\x64\bin\llvm-rc.exe --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\Llvm\x64\bin\lld-link.exe /nologo CMakeFiles\cmTC_7f259.dir\testCCompiler.c.obj  /out:cmTC_7f259.exe /implib:cmTC_7f259.lib /pdb:cmTC_7f259.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    MT: command "CMAKE_MT-NOTFOUND /nologo /manifest CMakeFiles\cmTC_7f259.dir/intermediate.manifest /out:CMakeFiles\cmTC_7f259.dir/embed.manifest /notify_update" failed (exit code 0x0) with the following output:
    The system cannot find the file specified
    ninja: build stopped: subcommand failed.

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)```

Additional Information

All discussed above, I did try the following but this didn't work image

gcampbell-msft commented 8 months ago

Thanks for notifying us of this @Wolfleader101! I am marking this as a bug and I also will mark it so that it doesn't ever get closed if there isn't activity on it, since it seems like #2798 never got fixed, but was closed due to inactivity.

We likely won't get to this until after the holiday season, but I will make sure that it is on our backlog!

cctv130 commented 5 months ago

The key to this problem is that we should call vcvars64.bat before executing cmake

gcampbell-msft commented 5 months ago

@Wolfleader101 I'm curious if this works if you update to the most recent version of the extension? I believe this should be supported for presets and generic configure and build. We should be pulling in the VS Dev Env, and if we're not it may be a bug.

If you could zip up a minimal repro (CMakeLists.txt included, this may have information that is relevant), that would be great.

Wolfleader101 commented 5 months ago
        "CMAKE_C_COMPILER": "clang-cl.exe",
        "CMAKE_CXX_COMPILER": "clang-cl.exe"

Hi @gcampbell-msft I am still getting the issue even with the latest version of the extension.

A reproducible demo can be found at my game Engine (Libraries haven't been included in libs folder as sub modules as they are in testing, but it should fail before it gets to complaining that it can't find the source files for the libs anyway) https://github.com/Wolfleader101/FenrirEngine/

You will need to change the CmakePresets.json in Windows base to use clang-cl.exe, though

 {
      "name": "windows-clang-base",
      "hidden": true,
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/out/build/${presetName}",
      "installDir": "${sourceDir}/out/install/${presetName}",
      "cacheVariables": {
        "CMAKE_C_COMPILER": "clang-cl.exe",
        "CMAKE_CXX_COMPILER": "clang-cl.exe"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    },

Here is a zip with the libs and updated cmakepresets.

https://drive.google.com/file/d/10bLbcN2ho4gdVJNjAafycPVSn9DHtOif/view?usp=sharing

gcampbell-msft commented 5 months ago

@cctv130 What does vcvars64.bat add to the environment? I've tried running that and still don't see the required environment variables.

gcampbell-msft commented 5 months ago

@cctv130 @Wolfleader101 I believe I've found the issue. The first version that is found by the CMake Tools extension is what is used. If it is not finding clang, it is likely because the clang tools are not installed for that version of Visual Studio:

image

Could you all install these for the version of Visual Studio that is being used and let me know if that solves your issue?

Thanks!

Wolfleader101 commented 5 months ago

@cctv130 @Wolfleader101 I believe I've found the issue. The first version that is found by the CMake Tools extension is what is used. If it is not finding clang, it is likely because the clang tools are not installed for that version of Visual Studio:

image

Could you all install these for the version of Visual Studio that is being used and let me know if that solves your issue?

Thanks!

@gcampbell-msft I have clang tools installed, the config works in my Visual Studio, it's also why i am able to use clang.exe and clang++.exe, I just cant use clang-cl.

cctv130 commented 5 months ago

Start Menu Search x64 Native Tools Command Prompt for VS 2019 find the Right-click properties,Find the path of the file I don't know what environment variables are in vcvars64.bat, but I know this is optimal way clang-cl only depends on MSVC So even if your method works with clang-cl, it won't work with the latest version of LLVM

Wolfleader101 commented 5 months ago

pends on MSVC So even if your method works with clang-cl, it won't work with the latest version of LLVM

yeah, it requires vcars64.bat to be called, but i assume that gets called when using the MSVC as your compiler - so it doesn't make sense that when calling clang-cl its not calling vcvars64.bat....

gcampbell-msft commented 5 months ago

@Wolfleader101 @cctv130 Ah, I've investigate and found the issue. I'll place this on our backlog.

However, I think we should treat this as an enhancement. I believe CMake Tools hasn't ever supported this based on my recent testing of old versions, and the reason it's failing, from what I have investigated, is because of missing CXXFLAGS and CFLAGS. Visual Studio has code that automatically injects them, but they aren't part of the developer environment.

@cctv130 I've tested calling vcvars64.bat, and it still does not include the variables that allow this to succeed, it still fails. Are you experiencing something different?

If I add the following to my preset that is using clang-cl.exe

"environment": {
    "CXXFLAGS": "--target=amd64-pc-windows-msvc",
    "CFLAGS": "--target=amd64-pc-windows-msvc"
}, 

and ensure to delete the output folder and then re-configure the cache, it seems to succeed.

cctv130 commented 5 months ago

You need to double-click x64 Native Tools Command Prompt for VS 2019 to enter the command terminal, set up a custom clang-cl, and then execute the cmake -GNinja .. command.

cctv130 commented 5 months ago

Compiling projects outside of the x64 Native Tools command prompt for VS 2019 terminal will not rely on the environment variables of vcvars64.bat

Wolfleader101 commented 4 months ago

@Wolfleader101 @cctv130 Ah, I've investigate and found the issue. I'll place this on our backlog.

However, I think we should treat this as an enhancement. I believe CMake Tools hasn't ever supported this based on my recent testing of old versions, and the reason it's failing, from what I have investigated, is because of missing CXXFLAGS and CFLAGS. Visual Studio has code that automatically injects them, but they aren't part of the developer environment.

@cctv130 I've tested calling vcvars64.bat, and it still does not include the variables that allow this to succeed, it still fails. Are you experiencing something different?

If I add the following to my preset that is using clang-cl.exe

"environment": {
    "CXXFLAGS": "--target=amd64-pc-windows-msvc",
    "CFLAGS": "--target=amd64-pc-windows-msvc"
}, 

and ensure to delete the output folder and then re-configure the cache, it seems to succeed.

Any update on when this would fixed, or is it still in the backlog?

v-frankwang commented 4 months ago

@Wolfleader101 Thank you for following up. We don't have any updates on when this issue will be fixed, and thank you for your patience.

LVSTRI commented 2 months ago

Having had the same issue, adding the following lines to the clang-cl kit in cmake-tools-kits.json fixes the issue as well:

"visualStudio": "<hash>",
"visualStudioArchitecture": "<arch>",
zanaviska commented 2 months ago

@LVSTRI

Having had the same issue, adding the following lines to the clang-cl kit in cmake-tools-kits.json fixes the issue as well:

"visualStudio": "<hash>",
"visualStudioArchitecture": "<arch>",

How can I find out the correct values for <hash> and <arch>?

kelteseth commented 2 months ago

We had the same issue. Adding all the paths fixed it for us. Note: you must have a matching VCToolsVersion and WindowsSDKVersion!

     {
    "version": 4,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 18,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "default",
            "displayName": "default",
            "description": "default",
            "generator": "Ninja",
            "hidden": true
        },
        {
            "name": "default-64bit-windows",
            "displayName": "default 64bit MSVC",
            "description": "Windows only!",
            "hidden": true,
            "inherits": "default",
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            },
            "toolset": {
                "value": "host=x64",
                "strategy": "external"
            },
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "VCPKG_TARGET_TRIPLET": "x64-windows",
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            }
        },
        {
            "name": "windows-clang-base",
            "hidden": true,
            "inherits": "default-64bit-windows",
            "environment": {
                "VCToolsVersion": "14.40.33807",
                "WindowsSDKVersion": "10.0.22621.0",
                "VCArch": "x64",
                "VCHostArch": "Hostx64",
                "VCToolsInstallDir": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/$env{VCToolsVersion}",
                "WindowsSdkDir": "C:/Program Files (x86)/Windows Kits/10",
                "WindowsSdkIncVerDir": "$env{WindowsSdkDir}/Include/$env{WindowsSDKVersion}",
                "WindowsSdkLibVerDir": "$env{WindowsSdkDir}/Lib/$env{WindowsSDKVersion}",
                "INCLUDE": "$env{VCToolsInstallDir}/ATLMFC/include;$env{VCToolsInstallDir}/include;$env{WindowsSdkIncVerDir}/ucrt;$env{WindowsSdkIncVerDir}/shared;$env{WindowsSdkIncVerDir}/um;$env{WindowsSdkIncVerDir}/winrt;$env{WindowsSdkIncVerDir}/cppwinrt",
                "LIB": "$env{VCToolsInstallDir}/ATLMFC/lib/$env{VCArch};$env{VCToolsInstallDir}/lib/$env{VCArch};$env{WindowsSdkLibVerDir}/ucrt/$env{VCArch};$env{WindowsSdkLibVerDir}/um/$env{VCArch}",
                "PATH": "$env{aqt_path}/Tools/Ninja/;$env{VCToolsInstallDir}/bin/$env{VCHostArch}/$env{VCArch};$env{WindowsSdkDir}/bin/$env{WindowsSDKVersion}/$env{VCArch};$penv{PATH}"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_C_COMPILER": "C:/Program Files/LLVM/bin/clang-cl.exe",
                "CMAKE_CXX_COMPILER": "C:/Program Files/LLVM/bin/clang-cl.exe"
            }
        },
        {
            "name": "windows-clang-debug-qt-6.7.1",
            "hidden": false,
            "inherits": "windows-clang-base",
            "displayName": " 6.7.1 Clang 64bit Debug",
            "binaryDir": "${sourceDir}/../build_CLANG_64bit_Debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        }
}
Wolfleader101 commented 2 weeks ago

Yep, those paths are meant to be set by vcars64.bat