From 27844ba8aa4c70b4af10bc9f298eb1f83c4d2a45 Mon Sep 17 00:00:00 2001 From: Steffen Siering Date: Mon, 7 Oct 2024 10:17:52 +0200 Subject: [PATCH] build: add support for C source files to Project (#98) 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. --- src/pgzx/build.zig | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/pgzx/build.zig b/src/pgzx/build.zig index e064cfd..5657814 100644 --- a/src/pgzx/build.zig +++ b/src/pgzx/build.zig @@ -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(); @@ -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, @@ -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), }; } @@ -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; @@ -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 {