Skip to content

Commit

Permalink
build: add support for C source files to Project (#98)
Browse files Browse the repository at this point in the history
Add direct C support to `Build.Project`. This allows one to mix existing
C code with new Zig code when writing a Zig based extension.

Example usage:

```
    var proj = PGBuild.Project.init(b, .{
        .name = "my_extension",
        .version = .{ .major = 0, .minor = 1 },
        .root_dir = "./src",
    });
    proj.addIncludePath(b.path("path/to/c/include"));
    proj.addCSourceFiles(.{
        .files = &[_][]const u8{
            "path/to/c/code.c",
        },
    });
```

The C configuration will be applied to the shared library build by the
project.
  • Loading branch information
urso authored Oct 7, 2024
1 parent 3f6f4a1 commit 27844ba
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/pgzx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");

const Step = std.Build.Step;
const LazyPath = std.Build.LazyPath;
const AddCSourceFilesOptions = std.Build.Module.AddCSourceFilesOptions;

const Build = @This();

Expand Down Expand Up @@ -50,6 +51,11 @@ pub const Project = struct {
options: std.StringArrayHashMapUnmanaged(*Step.Options),
config: Config,

// C support
includePaths: std.ArrayList(LazyPath),
libraryPaths: std.ArrayList(LazyPath),
cSourcesFiles: std.ArrayList(AddCSourceFilesOptions),

pub const Config = struct {
name: []const u8,
version: ExtensionVersion,
Expand Down Expand Up @@ -88,6 +94,9 @@ pub const Project = struct {
},
.config = proj_config,
.options = std.StringArrayHashMapUnmanaged(*Step.Options).init(b.allocator, &.{}, &.{}) catch unreachable,
.includePaths = std.ArrayList(LazyPath).init(b.allocator),
.libraryPaths = std.ArrayList(LazyPath).init(b.allocator),
.cSourcesFiles = std.ArrayList(AddCSourceFilesOptions).init(b.allocator),
};
}

Expand All @@ -98,11 +107,22 @@ pub const Project = struct {
.root_dir = proj.config.root_dir,
.root_source_file = proj.build.path(proj.config.root_source_file.?),
});
lib.root_module.addImport("pgzx", proj.deps.pgzx);
var mod = &lib.root_module;
mod.addImport("pgzx", proj.deps.pgzx);

var it = proj.options.iterator();
while (it.next()) |kv| {
lib.root_module.addOptions(kv.key_ptr.*, kv.value_ptr.*);
mod.addOptions(kv.key_ptr.*, kv.value_ptr.*);
}

for (proj.includePaths.items) |path| {
mod.addIncludePath(path);
}
for (proj.libraryPaths.items) |path| {
mod.addLibraryPath(path);
}
for (proj.cSourcesFiles.items) |options| {
mod.addCSourceFiles(options);
}

return lib;
Expand All @@ -119,6 +139,18 @@ pub const Project = struct {
pub fn addOptions(proj: *Project, module_name: []const u8, options: *Step.Options) void {
proj.options.put(proj.build.allocator, module_name, options) catch unreachable;
}

pub fn addIncludePath(proj: *Project, path: LazyPath) void {
proj.includePaths.append(path) catch unreachable;
}

pub fn addLibraryPath(proj: *Project, path: LazyPath) void {
proj.libraryPaths.append(path) catch unreachable;
}

pub fn addCSourceFiles(proj: *Project, options: AddCSourceFilesOptions) void {
proj.cSourcesFiles.append(options) catch unreachable;
}
};

pub const DebugOptions = struct {
Expand Down

0 comments on commit 27844ba

Please sign in to comment.