hedronvision / bazel-compile-commands-extractor

Goal: Enable awesome tooling for Bazel users of the C language family.
Other
696 stars 112 forks source link

compile_commands.json doesnt find system headers. #221

Open Zeed-gn opened 2 months ago

Zeed-gn commented 2 months ago

I'm using https://github.com/bazel-contrib/toolchains_llvm to set up the toolchain the project should use and making sure everything looks right. Everything compiles, and the compilecommands.json seems to generated, mostly correct. Except that the system headers (libc, libc++, etc) are being pulled from the host instead from the bazel-.../external/toolchain path. So C++20 headers are not found.

There is no sysroot flag on the compile_commands, so I will assume that make clangd use whatever the host is using.

Is there something else I need to set?

Zeed-gn commented 2 months ago

Screenshot from 2024-09-19 09-06-01 Actually just tried with the host(gcc), seems like the certain paths are available, but the C++ header path is not properly added into the compile_commands.json. While bazel resolves evertyhing nicely, the compile commands for a sample binary:

cc_binary(
     name = "foo",
     srcs = ["foo.cc"]
  )

foo.cc

  #include <vector>
  int main(int, char**) { return 0;}

Generates the following compile_commands.json

    {
    "file": "foo.cc",
    "arguments": [
      "/usr/bin/gcc",
      "-xc++",
      "-U_FORTIFY_SOURCE",
      "-fstack-protector",
      "-Wall",
      "-Wunused-but-set-parameter",
      "-Wno-free-nonheap-object",
      "-fno-omit-frame-pointer",
      "-std=c++14",
      "-MD",
      "-MF",
      "bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.d",
      "-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.o",
      "-fPIC",
      "-iquote",
      ".",
      "-iquote",
      "bazel-out/k8-fastbuild/bin",
      "-std=c++20",
      "-Wall",
      "-Werror",
      "-Wextra",
      "-Wextra-semi",
      "-Wconversion",
      "-Wno-unused-parameter",
      "-Wold-style-cast",
      "-Wwrite-strings",
      "-Wimplicit-fallthrough",
      "-fno-exceptions",
      "-fno-rtti",
      "-fomit-frame-pointer",
      "-funwind-tables",
      "-Wno-builtin-macro-redefined",
      "-D__DATE__=\"redacted\"",
      "-D__TIMESTAMP__=\"redacted\"",
      "-D__TIME__=\"redacted\"",
      "-c",
      "foo.cc",
      "-o",
      "bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.o"
    ],
    "directory": "/home/user/repos/new_repo"
  },

Now clangd fail to find though <c++/13/vector> exists.

Zeed-gn commented 2 months ago

Curerrent compile command commit: "1e08f8e0507b6b6b1f4416a9a22cf5c28beaba93" (HEAD) Current bazel version: "7.3.1"

Zeed-gn commented 2 months ago

Hacking out a local repo, that injects -isystem for each builtin directory seems to work. Though have to strip nested external, since it seems that external repositories that depend on each other end up generating nested external paths. repo1 being llvm_toolchain and repo2 being llvm_toolchain_llvm, in the llvm_toolchain mod. There is probably a less hacky way to do this.

external/repo1/external/repo2/path_to_bin.

external/toolchains_llvm~~llvm~llvm_toolchain/bin/cc_wrapper.sh -xc++ -U_FORTIFY_SOURCE --target=x86_64-unknown-linux-gnu -U_FORTIFY_SOURCE -fstack-protector -fno-omit-frame-pointer -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -std=c++17 -stdlib=libc++ -Xclang -fno-cxx-modules -MD -MF bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.d -frandom-seed=bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.o -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -std=c++20 -Wall -Werror -Wextra -Wextra-semi -Wconversion -Wno-unused-parameter -Wold-style-cast -Wwrite-strings -Wimplicit-fallthrough -fno-exceptions -fno-rtti -fomit-frame-pointer -funwind-tables -no-canonical-prefixes -Wno-builtin-macro-redefined "-D__DATE__=\"redacted\"" "-D__TIMESTAMP__=\"redacted\"" "-D__TIME__=\"redacted\"" -c -o bazel-out/k8-fastbuild/bin/_objs/foo/foo.pic.o -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/include/c++/v1 -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/include/x86_64-unknown-linux-gnu/c++/v1 -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib/clang/19.1.0/include -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib/clang/19.1.0/share -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib64/clang/19.1.0/include -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib/clang/19/include -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib/clang/19/share -Iexternal/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib64/clang/19/include -I/usr/include -I/usr/local/include -resource-dir=/hom_path/.cache/bazel/_bazel_user/85ee35e2f2022d11c5822a0c418173da/external/toolchains_llvm~~llvm~llvm_toolchain_llvm/lib/clang/19 -- /path_to_repo/foo.cc