feat: generate rust-project.json for LSP
Add a CMake module that writes rust-project.json on every configure so rust-analyzer can resolve the no-Cargo crate layout, sysroot crates, and proc-macro attributes without Cargo. Co-Authored-By: qwen (lmstudio/qwen3.8-27b@q3_k_xl): LSP setup
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# Generate rust-project.json for rust-analyzer (LSP) support.
|
||||
#
|
||||
# This project has no Cargo manifest, so rust-analyzer cannot discover the
|
||||
# crate layout on its own. The generated file describes the three crates and
|
||||
# points at the toolchain sysroot so core/alloc resolve. It is regenerated on
|
||||
# every configure; the output is git-ignored.
|
||||
|
||||
if(NOT RUSTC)
|
||||
find_program(RUSTC rustc REQUIRED)
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND "${RUSTC}" --print sysroot
|
||||
OUTPUT_VARIABLE RUST_SYSROOT_RAW
|
||||
RESULT_VARIABLE RUST_SYSROOT_RESULT
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
|
||||
if(NOT RUST_SYSROOT_RESULT EQUAL 0 OR NOT RUST_SYSROOT_RAW)
|
||||
message(FATAL_ERROR "Failed to query the rustc sysroot")
|
||||
endif()
|
||||
|
||||
# Use forward slashes so the JSON needs no backslash escaping.
|
||||
string(REPLACE "\\" "/" RUST_SYSROOT "${RUST_SYSROOT_RAW}")
|
||||
set(RUST_SYSROOT_SRC "${RUST_SYSROOT}/lib/rustlib/src/rust/library")
|
||||
|
||||
if(NOT IS_DIRECTORY "${RUST_SYSROOT_SRC}")
|
||||
message(WARNING "rust-src not found at ${RUST_SYSROOT_SRC}. "
|
||||
"Run `rustup component add rust-src` for full LSP support.")
|
||||
endif()
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_LIST_DIR}/rust-project.json.in"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/rust-project.json"
|
||||
@ONLY
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"sysroot": "@RUST_SYSROOT@",
|
||||
"sysroot_src": "@RUST_SYSROOT_SRC@",
|
||||
"crates": [
|
||||
{
|
||||
"display_name": "rslibc",
|
||||
"root_module": "src/lib.rs",
|
||||
"edition": "2021",
|
||||
"deps": []
|
||||
},
|
||||
{
|
||||
"display_name": "rslibc_macros",
|
||||
"root_module": "macros/src/lib.rs",
|
||||
"edition": "2021",
|
||||
"is_proc_macro": true,
|
||||
"deps": []
|
||||
},
|
||||
{
|
||||
"display_name": "rslibc_example",
|
||||
"root_module": "runtime/main.rs",
|
||||
"edition": "2021",
|
||||
"deps": [
|
||||
{ "crate": 0, "name": "rslibc" },
|
||||
{ "crate": 1, "name": "rslibc_macros" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user