Azure / azure-sdk-for-c

This repository is for active development of the Azure SDK for Embedded C. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-c.
MIT License
218 stars 115 forks source link

Can't use VSCode with CMake #916

Closed CIPop closed 3 years ago

CIPop commented 3 years ago

Describe the bug CMake from VSCode isn't working anymore.

Exception or Stack Trace The cmake extension doesn't work (previously working fine) in Windows. (original stack/error fixed - see comments for reference).

To Reproduce On Windows + cmake version 3.17.2 + Powershell (Windows Terminal) or cmd.exe: Steps to reproduce the behavior:

  1. follow instructions in https://github.com/Azure/azure-sdk-for-c#development-environment.
  2. follow instructions in https://github.com/Azure/azure-sdk-for-c#vscode

Code Snippet

  1. Set all required environment variables.

In Windows I do this with Invoke-BatchScript, on Linux I dot-source a bash script.

  1. Follow the cmake steps in command-prompt. In the \build folder:

cmake .. or cmake-gui .. or ccmake ..

  1. Build using your preferred tool-chain

msbuild /m ./az.sln or make -j

  1. Open Visual Studio Code (with the CMake plugin installed) from the same command-prompt. (code.exe will inherit the command-line environment, still in \build).

code ..

  1. Select "Configure All Projects". image

(Make sure to select an x64 toolset on Windows - autodetect will select the x86 toolset instead.)

Expected behavior VSCode auto-detects and uses the configuration I've specified in command-line.

Actual behavior:

VSCode replaces the configuration specified in command-line.

Screenshots CMake-Gui option-buttons are broken by VSCode, configuration is replaced by something else:

My configuration: image

After VSCode configured cmake: image

None of the samples are getting built or can be debugged.

Setup (please complete the following information):

Additional context

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

CIPop commented 3 years ago

CMakeOutput.log

Cmake from Windows Terminal / Powershell works fine.

danewalton commented 3 years ago

hmm I was just able to run it fine. Are you able to post what your settings.json file looks like?

CIPop commented 3 years ago

@danewalton the settings.json that is checked in.

To be clear I was able to do the following in the past:

  1. Go through the prerequisites (which expect setting environment variables for Visual Studio compilers and VCPkg).
  2. In the same command prompt use cmake and cmake-gui to configure the project.
  3. Open Visual Studio Code then do "configure" - this will detect my configuration from the previous step.
  4. I would be able to continue to use cmake-gui and VSCode interchangeable.

I can do this for our other repo at https://github.com/Azure/azure-iot-sdk-c.

I've already tried to remove the .vscode folder from master. Even if I do that, VSCode won't pick up my cmake/cmake-gui configuration for some reason.

CIPop commented 3 years ago

The settings.json from master won't work either. I think this has something to do with how VSCode vs cmake is configuring the project.

Here's settings.json with my changes:

{
  "cmake.configureEnvironment": {
    "VCPKG_ROOT": "s:\vcpkg",
    "VCPKG_DEFAULT_TRIPLET": "x64-windows-static"
  },
  "cmake.configureSettings": {
    "WARNINGS_AS_ERRORS" : "ON",
    "TRANSPORT_CURL" : "OFF",
    "UNIT_TESTING" : "ON",
    "UNIT_TESTING_MOCKS" : "OFF",
    "TRANSPORT_PAHO" : "ON",
    "PRECONDITIONS" : "ON",
    "LOGGING" : "ON"
  },
  "cmake.debugConfig": {
    "env": {
      "AZ_IOT_DEVICE_ID": "",
      "AZ_IOT_HUB_HOSTNAME": "",

      "AZ_IOT_ID_SCOPE": "",
      "AZ_IOT_REGISTRATION_ID": "",

      "AZ_IOT_DEVICE_X509_CERT_PEM_FILE": "",
      "AZ_IOT_DEVICE_X509_TRUST_PEM_FILE": "",

      "AZ_IOT_HUB_DEVICE_SAS_KEY": "",
      "AZ_IOT_HUB_DEVICE_SAS_KEY_DURATION": ""
    }
  }
}

This fails with

[main] Configuring folder: c 
[proc] Executing command: "c:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DWARNINGS_AS_ERRORS:STRING=ON -DTRANSPORT_CURL:STRING=OFF -DUNIT_TESTING:STRING=ON -DUNIT_TESTING_MOCKS:STRING=OFF -DTRANSPORT_PAHO:STRING=ON -DPRECONDITIONS:STRING=ON -DLOGGING:STRING=ON -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -Hs:/c -Bs:/c/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeDetermineSystem.cmake:99 (message):
[cmake]   Could not find toolchain file: s:cpkg/scripts/buildsystems/vcpkg.cmake
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:47 (project)
[cmake] -- Configuring incomplete, errors occurred!
[cmake] 
[cmake] 
[cmake] CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
[cmake] CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
danewalton commented 3 years ago

Seems at least part of the problem was mismatched compiler version. I updated the instructions in #920 to hopefully call that out and prevent the same in the future.

CIPop commented 3 years ago

920 is a workaround but the existence of the .vscode folder is interfering when working in a mixed command-line vs VSCode mode.

If I do cmake .. or ccmake .. in command line, I expect that VSCode will not alter the configuration.

CIPop commented 3 years ago

Closed incorrectly (I missed my comment in the PR).

CIPop commented 3 years ago

Original issue was fixed and docs updated by @danewalton :

[variant] Loaded new set of variants
[kit] Successfully loaded 4 kits from C:\Users\crist\AppData\Local\CMakeTools\cmake-tools-kits.json
[main] Configuring folder: c 
[cmakefileapi-driver] Removing s:/c/build/CMakeCache.txt
[proc] Executing command: "c:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DWARNINGS_AS_ERRORS:STRING=ON -DTRANSPORT_CURL:STRING=OFF -DUNIT_TESTING:STRING=OFF -DUNIT_TESTING_MOCKS:STRING=OFF -DTRANSPORT_PAHO:STRING=OFF -DPRECONDITIONS:STRING=ON -DLOGGING:STRING=ON -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -Hs:/c -Bs:/c/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeDetermineSystem.cmake:99 (message):
[cmake]   Could not find toolchain file: /scripts/buildsystems/vcpkg.cmake
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:47 (project)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "S:/c/build/CMakeFiles/CMakeOutput.log".
[cmake] CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
[cmake] CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage

To fix this, ensure that VSCode selects an x64 cmake toolset. Auto-detect (or not specifying one) will end up using x86 which causes the error above.

CIPop commented 3 years ago

@danewalton my original proposal is to rename/remove .vscode to avoid interference with cmake as well as personal settings. I think the checked-in files are only templates and cannot be ported between Win/Lin/Mac configurations as they are defining VCPKG root and triplet.

The issues with the current .vscode/settings.json are that:

  1. It interferes with the cmd-line build system and our own prerequisite documentation, creating a confusion between cmd-line and IDE builds.
  2. Require modification to configure and run samples. Modifications will be marked as tracked changes:
    
    git status
    On branch master
    Your branch is up to date with 'origin/master'.

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: ../.vscode/settings.json


3. It may interfere/overwrite with other VSCode personalization for this repo.
4. Breaks cmake-gui / ccmake enable/disable feature for options.