Skip to content

the-argus/zig-compile-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig compile_commands.json

A simple zig module to generate compile_commands.json from a slice of build targets. Useful if you are using zig as a build system for C/C++.

Supports zig v0.15.1 and v0.16.0 Older versions are available in the commit history, but not maintained.

Example Usage

To get the package in your project, cd into its root directory and run:

zig fetch --save-exact=compile_commands "https://github.com/the-argus/zig-compile-commands/archive/9400cd1963ea6bb58fe47ba7d9700075b808cdd2.tar.gz"

This will add an entry in your build.zig.zon with the hash of the commit in that link (the 0.15.1/0.16.0 version).

The next step is to use it into your build.zig by use @import on the dependency (zig compile commands is not a normal zig dependency, it is intended to be used as a build-time zig library):

// import the dependency
const zcc = @import("compile_commands");

pub fn build(b: *std.Build) !void {
    // make a list of targets that have include files and c source files
    var targets: std.ArrayList(*std.Build.Step.Compile) = .empty;

    // create your executable
    const exe = b.addExecutable(.{
        .name = "my-project",
        .root_module = b.createModule(.{
            .target = target,
            .optimize = optimize,
        }),
    });
    // keep track of it, so later we can pass it to compile_commands
    targets.append(b.allocator, exe) catch @panic("OOM");
    // maybe some other targets, too?
    targets.append(b.allocator, exe_2) catch @panic("OOM");
    // if this is an output, append it. but if exe or exe_2 links it, then it
    // will get pulled in automatically
    targets.append(b.allocator, lib) catch @panic("OOM");

    // add a step called "cdb" (Compile commands DataBase) for making
    // compile_commands.json. could be named anything. cdb is just quick to type
    _ = zcc.createStep(b, "cdb", targets.toOwnedSlice(b.allocator) catch @panic("OOM"));
}

And you're all done. Just run zig build cdb to generate the compile_commands.json file according to your current build graph.

If building compile_commands.json panics

You probably have source files being generated by a build step (maybe a config header?) and zig can't know the paths for those source files without building them first. To resolve it, you can create your build step like so:

    const targetsSlice = targets.toOwnedSlice(b.allocator) catch @panic("OOM");
    const buildStep = zcc.createStep(b, "cdb", targetsSlice);
    // Build everything in the project before generating the compile_commands
    for (targetsSlice) |target| step.dependOn(&target.step);

In the future, zig-compile-commands should automatically depend on all relevant file generation steps. For now, it is up to the user to find the relevant steps and manually insert dependencies. The easiest way to do this is to depend on all compile steps, as in the above example.

About

A simple zig module to generate compile_commands.json from a slice of build targets.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors