microsoft / microsoft/vscode-cmake-tools
A Kit with toolchainFile fails to determine CMAKE_(C|CXX)_COMPILER without the user adding both variables to the cache file
- Dominant language
- TypeScript
- Stars
- 1.7k
- Forks
- 546
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 32
Description
### Brief Issue Summary
A local user CMake kit using a toolchainFile will make cmake-tools fail at determining the C/C++ compiler path.
Then IntelliSense setup is done wrong:
1. Architecture/Pltform/CompilerFlavor dependent codepaths highlighting is wrong (eg: `__amd64__` or `__arm__`, `_WIN32` or `__linux__`, `__MINGW32__` or `__MINGW64__`)
2. Code completion is made difficult or incorrect (because of 1., symbols may be missing etc...)
### Expected:
CMake [toolchain file documentation](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html) doesn't mandate that `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` variables be placed in the cache.
`CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` should be figured out automatically by the extension w/o adding a cache entry for them both.
Note that the cache workaround is mentioned in:
- #602 where writing the CXX/C compiler settings forces them on the command line and thus in the cache
- #500 where a specific pair of `set(... CACHE ...)` calls are inserted in the toolchain file
### Reproduction steps:
1. Setup a local CMake build kit using a toolchain file documented as per CMake guidelines. (eg: a buildroot generated toolchain file will do just fine)
2. Import a C++ project for which code blocks vary depending on the architecture (eg: `__amd64__` or `__arm__`) or on the platform (eg: `_WIN32` or `__linux__`) or the compiler flavor (eg: `__MINGW32__` or `__MINGW64__`)
3. `Ctrl+Shift+P` `Cmake: Configure`
4. Observe the codepaths highlighting mismatches and the code completion problems
### Apparent Behavior:
CMake Tools expects `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` in the cache to setup the CPP project correctly.
### CMake Tools Log
No relevant logs to copy/paste.
### Developer Tools Log
No relevant logs to copy/paste.
### Platform and Versions
- **Operating System**: Debian GNU/Linux SID
- **CMake Version**: 3.16.3
- **VSCode Version**: Version: 1.44.2 Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd
- **CMake Tools Extension Version**: 1.3.1
- **Compiler/Toolchain**: any cross toolchain would exhibit the problem as long as it's setup only through a toolchain file. (tested with a GCC 7.3 generated by a ARM based buildroot build, or Debian's (g++|gcc)-arm-linux-gnueabi packages, or Debian's gcc-mingw-w64-(x86-64|i686))
### Other Notes/Information
Here is a failing toolchain file example. It targets mingw32 builds using mingw32 debian packages.
```cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x86)
set(toolchain_dir "/usr/bin")
set(toolchain_triplet "i686-w64-mingw32")
set(toolchain_prefix "${toolchain_triplet}-")
set(toolchain_suffix "-posix")
set(CMAKE_SYSROOT "/usr/${toolchain_triplet}")
set(CMAKE_C_COMPILER "${toolchain_dir}/${toolchain_prefix}gcc${toolchain_suffix}")
set(CMAKE_CXX_COMPILER "${toolchain_dir}/${toolchain_prefix}g++${toolchain_suffix}")
set(CMAKE_RC_COMPILER "${toolchain_dir}/${toolchain_prefix}windres" CACHE FILEPATH "" FORCE)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
```
Appending these two lines are the current workaround for hand made toolchain files
```cmake
set(CMAKE_C_COMPILER "${CMAKE_C_COMPILER}" CACHE FILEPATH "C Compiler path")
set(CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}" CACHE FILEPATH "C++ Compiler path")
```
The problem can't be workaround easily when using generated toolchain files (eg: buildroot ./host/usr/share/buildroot/toolchainfile.cmake) or external toolchain files provided by third parties.
Contributor guide
Research direction
Start with the CMake: Configure flow and the extension's compiler discovery when a toolchain file sets CMAKE_C_COMPILER and CMAKE_CXX_COMPILER without cache entries. Reproduce with the provided MinGW or Buildroot-style toolchain file, then verify that compiler paths and IntelliSense configuration are correct without adding those cache variables.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, typescript
- Domain
- build-system, developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100