microsoft / vscode-cmake-tools

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

Scan for kits on OSX fails to setup archive tool properly #3190

Closed thirtytwobits closed 1 year ago

thirtytwobits commented 1 year ago

Brief Issue Summary

When using vscode on osx, and when installing compilers via brew, the configuration fails to configure CMAKE_AR which causes incompatibilities between .a files and their consumers.

Output like this is indicative:

[build] ld: warning: ignoring file lib/libgmock_main.a, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )

The same system can build successfully by manually setting CMAKE_AR.

Steps to Reproduce (OSX-only bug)

  1. install xcode on your system
  2. brew install gcc
  3. brew install llvm
  4. create a C++ project that creates .a files and links these into an executable.
  5. select "scan for kits" in the cmake tools build kits menu. It should find the version of llvm installed by brew.
  6. Select the llvm version discovered in the brew keg directory (/usr/local/opt)
  7. configure and build

Expected

Build is successful.

Actual

the linker ignores the .a files because it does not recognize their format.

CMake Tools Diagnostics

{
  "os": "darwin",
  "vscodeVersion": "1.78.2",
  "cmtVersion": "1.14.31",
  "configurations": [
    {
      "folder": "/path/to/CETL",
      "cmakeVersion": "3.25.3",
      "configured": true,
      "generator": "Ninja",
      "usesPresets": false,
      "compilers": {
        "C": "/usr/local/opt/llvm/bin/clang",
        "CXX": "/usr/local/opt/llvm/bin/clang++"
      }
    }
  ],
  "cpptoolsIntegration": {
    "isReady": true,
    "hasCodeModel": true,
    "activeBuildType": "Debug",
    "buildTypesSeen": [
      "Debug"
    ],
    "requests": [],
    "responses": [],
    "partialMatches": [],
    "targetCount": 87,
    "executablesCount": 22,
    "librariesCount": 27,
    "targets": []
  },
  "settings": [
    {
      "communicationMode": "automatic",
      "useCMakePresets": "auto",
      "configureOnOpen": false
    }
  ]
}

### Debug Log

```shell
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /path/to/CETL/build --config Debug --target run_test_variable_length_array_detailed_allocation --verbose --
[build] [1/2   0% :: 0.000] /usr/local/Cellar/cmake/3.25.3/bin/cmake -P /path/to/CETL/build/CMakeFiles/VerifyGlobs.cmake
[build] [5/11   9% :: 0.217] /usr/local/opt/llvm/bin/clang -DCETL_ENABLE_DEBUG_ASSERT=1 -I/path/to/CETL/cetlvast/include -I/path/to/CETL/cetlvast/build_external/o1heap/o1heap -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -fPIC -pedantic -Wall -Wextra -Werror -Wfloat-equal -Wconversion -Wunused-parameter -Wunused-variable -Wunused-value -Wcast-align -Wmissing-declarations -Wmissing-field-initializers -Wdouble-promotion -Wswitch-enum -Wtype-limits -Wno-error=array-bounds -O0 -DDEBUG -ggdb -MD -MT suites/unittest/CMakeFiles/o1heap.dir/__/__/build_external/o1heap/o1heap/o1heap.c.o -MF suites/unittest/CMakeFiles/o1heap.dir/__/__/build_external/o1heap/o1heap/o1heap.c.o.d -o suites/unittest/CMakeFiles/o1heap.dir/__/__/build_external/o1heap/o1heap/o1heap.c.o -c /path/to/CETL/cetlvast/build_external/o1heap/o1heap/o1heap.c
[build] [6/11  18% :: 0.361] : && /usr/local/Cellar/cmake/3.25.3/bin/cmake -E rm -f suites/unittest/libo1heap.a && /usr/local/opt/binutils/bin/ar qc suites/unittest/libo1heap.a  suites/unittest/CMakeFiles/o1heap.dir/__/__/build_external/o1heap/o1heap/o1heap.c.o && /usr/local/opt/llvm/bin/llvm-ranlib suites/unittest/libo1heap.a && /usr/local/Cellar/cmake/3.25.3/bin/cmake -E touch suites/unittest/libo1heap.a && :

Notice the use of /usr/local/opt/binutils/bin/ar in this command. This should be /usr/local/opt/llvm/bin/llvm-ar.



### Additional Information

Using this toolchain file works around the issue:

#
# Copyright (C) OpenCyphal Development Team  <opencyphal.org>
# Copyright Amazon.com Inc. or its affiliates.
# SPDX-License-Identifier: MIT
#

set(CMAKE_CXX_COMPILER_ID "Clang")
set(CMAKE_C_COMPILER_ID "Clang")
set(CMAKE_C_COMPILER /usr/local/opt/llvm/bin/clang)
set(CMAKE_CXX_COMPILER /usr/local/opt/llvm/bin/clang++)
set(CMAKE_ASM_COMPILER /usr/local/opt/llvm/bin/clang)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_LINKER_FLAGS_INIT
        "-L/usr/local/opt/llvm/lib/c++ -Wl,-rpath,/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib"
)
set(CMAKE_C_LINKER_FLAGS_INIT
        "-L/usr/local/opt/llvm/lib/c -Wl,-rpath,/usr/local/opt/llvm/lib/c -L/usr/local/opt/llvm/lib"
)
set(CMAKE_CXX_FLAGS_INIT
        "-I/usr/local/opt/llvm/include"
)

# The auto-scan for tools in vscode fails to properly setup the AR tool. This
# toolchain file fixes that.
set(CMAKE_AR /usr/local/opt/llvm/bin/llvm-ar)
thirtytwobits commented 1 year ago

Ah, nevermind. I found the problem. I had a version of binutils installed that was being pulled in by cmake itself. Mia culpa.