使用 clangd 設定 LSP
背景
Emacs、Vim 或 VS Code 等編輯器透過 LSP (語言伺服器通訊協定) 支援程式碼導覽、程式碼補全、內嵌編譯器錯誤訊息等功能。支援 LSP 的常見語言伺服器是 clangd,這類伺服器依賴 compile_commands.json (JSON 檔案,記錄專案中每個檔案的編譯指令)。
如何為 XLA 原始碼產生 compile_commands.json?
使用 build_tools/lint/generate_compile_commands.py 指令碼。從 XLA 存放區根目錄進行下列叫用,即可產生 compile_commands.json 檔案:bazel aquery "mnemonic(CppCompile,
//xla/...)" --output=jsonproto | python3
build_tools/lint/generate_compile_commands.py
建構清除器
Google 內部的 XLA CI 會執行額外檢查,確認所有目標都正確列出 BUILD 檔案中的所有依附元件,但 OSS Bazel CI 預設不會啟用這項檢查。在將 PR 送交 XLA 團隊之前執行下列指令,可大幅加快合併 PR 的時間,否則您需要請 Google 員工在內部為您修正這些問題,或根據 Google 員工的回饋進行幾輪 PR 審查,才能修正問題。
圖層檢查
使用 --features=layering_check 建構時,可確保您不會透過遞移依附元件意外納入標頭,且不會在目標依附元件中列出標頭
從 BUILD 檔案中移除未使用的依附元件
安裝 Buildozer 工具:
sudo curl -fsSL -o /usr/bin/buildozer https://github.com/bazelbuild/buildtools/releases/download/6.0.0/buildozer-linux-amd64
sudo chmod 755 /usr/bin/buildozer
安裝 Bant 工具:
# To some writable directory that does not require root access
bazel build -c opt //bant && install -D --strip bazel-bin/bant/bant ~/bin/bant
# For a system directory that requires root-access
sudo install -D --strip bazel-bin/bant/bant /usr/local/bin/bant
使用 bant 產生 buildozer 指令,移除未使用的依附元件:
bant dwyu //xla/core/collectives:symmetric_memory
如果運氣好,可以直接執行:
. <(bant dwyu //xla/core/collectives:symmetric_memory)