https://github.com/tomcat-42/zig-compile-commands
compile_commands.json generation for c projects using zig build
https://github.com/tomcat-42/zig-compile-commands
build c compile-commands compile-commands-json zig zig-build zig-build-system
Last synced: about 1 year ago
JSON representation
compile_commands.json generation for c projects using zig build
- Host: GitHub
- URL: https://github.com/tomcat-42/zig-compile-commands
- Owner: Tomcat-42
- License: mit
- Created: 2025-04-08T18:27:33.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T19:00:53.000Z (about 1 year ago)
- Last Synced: 2025-04-09T15:16:50.582Z (about 1 year ago)
- Topics: build, c, compile-commands, compile-commands-json, zig, zig-build, zig-build-system
- Language: Zig
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zig compile commands
Simple build module to generate a `compile_commands.json`
file from [clang compilation database fragments](https://reviews.llvm.org/D66555) dir.
Note: I will only maintain support for zig master.
## Usage
Fetch the package:
```sh
zig fetch --save=compile_commands git+https://github.com/Tomcat-42/zig-compile-commands
```
Add the `-gen-cdb-fragment-path ` flag to your target. Here could be any dir,
for example the zig cache:
```zig
const flags: []const []const u8 = &.{
"-std=c23",
"-Wall",
"-Wextra",
"-Werror",
"-Wpedantic",
"-fno-strict-aliasing",
"-gen-cdb-fragment-path",
b.fmt("{s}/{s}", .{ b.cache_root.path.?, "cdb" }),
};
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.pic = true,
});
mod.addIncludePath(b.path("include"));
mod.addCSourceFiles(.{
.root = b.path("src"),
.files = &.{"main.c"},
.flags = flags,
});
const exe = b.addExecutable(.{
.name = "exe",
.root_module = mod,
});
```
Now, create the step. The arguments are the fragments dir(the same that you added to the flags)
and the resulting `compile_commands.json` path:
```zig
const cc_step = b.step("cc", "Generate Compile Commands Database");
const gen_file_step = try GenCompileCommands.createStep(
b,
b.fmt("{s}/{s}", .{ b.cache_root.path orelse "./", "cdb" }),
b.fmt("{s}/{s}", .{ b.cache_root.path orelse "./", "compile_commands.json" }),
);
gen_file_step.dependOn(&exe.step);
cc_step.dependOn(gen_file_step);
```
Finally, generate the file:
```sh
zig build cc
```