#llvm #clang #compile

compile_commands

Work with compile_commands.json and compile_flags.txt in Rust programs

4 releases (2 breaking)

0.3.0 Sep 4, 2024
0.2.0 Jul 16, 2024
0.1.1 Jul 14, 2024
0.1.0 Jul 10, 2024

#662 in Development tools

Download history 589/week @ 2024-09-14 505/week @ 2024-09-21 487/week @ 2024-09-28 476/week @ 2024-10-05 804/week @ 2024-10-12 480/week @ 2024-10-19 522/week @ 2024-10-26 445/week @ 2024-11-02 355/week @ 2024-11-09 441/week @ 2024-11-16 371/week @ 2024-11-23 372/week @ 2024-11-30 332/week @ 2024-12-07 354/week @ 2024-12-14 316/week @ 2024-12-21 233/week @ 2024-12-28

1,269 downloads per month
Used in asm-lsp

Apache-2.0

14KB
219 lines

Use compile_commands.json and compile_flags.txt in Rust Programs

Goal

Provide a thin wrapper type around the compile_commands.json and compile_flags.txt standards as provided by the LLVM project.

Sample usage

Given the following compile_commands.json file:

[
  { "directory": "/home/user/llvm/build",
    "arguments": ["/usr/bin/clang++", "-Irelative", "-DSOMEDEF=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
    "file": "file.cc" },

  { "directory": "/home/user/llvm/build",
    "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
    "file": "file2.cc" }
]

Or the following compile_flags.txt file:

-xc++
-I
libwidget/include/

Parse it and use as a type-safe object in your Rust project:

use std::path::PathBuf;

use compile_commands::CompilationDatabase;

fn main() {
    // Create a `CompilationDatabase` object directly from a compile_commands.json file
    let comp_cmds = include_str!("compile_commands.json");
    let comp_data = serde_json::from_str::<CompilationDatabase>(&comp_cmds).unwrap();
    _ = comp_data;

    // Or create a `CompilationDatabase` object from a compile_flags.txt file
    let comp_flags = include_str!("compile_flags.txt");
    let comp_data =
        compile_commands::from_compile_flags_txt(&PathBuf::from("~/foo/build"), &comp_flags);
    _ = comp_data;
}

Usage in the Wild

asm-lsp

Used to provide inline error diagnostics and additional per-project include directories

Dependencies

~0.7–1.6MB
~34K SLoC