From 6bc9c4f716afbb15465f79f84f61221f394fa5b6 Mon Sep 17 00:00:00 2001 From: Michael Buckley Date: Sat, 8 Jul 2023 21:38:27 -0400 Subject: [PATCH] std.Build: Add methods for creating modules from a TranslateC object. --- lib/std/Build/Step/TranslateC.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index ced249b3f2b3..60e35e940b11 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -65,6 +65,30 @@ pub fn addExecutable(self: *TranslateC, options: AddExecutableOptions) *Step.Com }); } +/// Creates a module from the translated source and adds it to the package's +/// module set making it available to other packages which depend on this one. +/// `createModule` can be used instead to create a private module. +pub fn addModule(self: *TranslateC, name: []const u8) *std.Build.Module { + return self.step.owner.addModule(name, .{ + .source_file = .{ .generated = &self.output_file }, + }); +} + +/// Creates a private module from the translated source to be used by the +/// current package, but not exposed to other packages depending on this one. +/// `addModule` can be used instead to create a public module. +pub fn createModule(self: *TranslateC) *std.Build.Module { + const b = self.step.owner; + const module = b.allocator.create(std.Build.Module) catch @panic("OOM"); + + module.* = .{ + .builder = b, + .source_file = .{ .generated = &self.output_file }, + .dependencies = std.StringArrayHashMap(*std.Build.Module).init(b.allocator), + }; + return module; +} + pub fn addIncludeDir(self: *TranslateC, include_dir: []const u8) void { self.include_dirs.append(self.step.owner.dupePath(include_dir)) catch @panic("OOM"); }