master
  1const std = @import("std");
  2const Step = std.Build.Step;
  3const LazyPath = std.Build.LazyPath;
  4const fs = std.fs;
  5const mem = std.mem;
  6
  7const TranslateC = @This();
  8
  9pub const base_id: Step.Id = .translate_c;
 10
 11step: Step,
 12source: std.Build.LazyPath,
 13include_dirs: std.array_list.Managed(std.Build.Module.IncludeDir),
 14c_macros: std.array_list.Managed([]const u8),
 15out_basename: []const u8,
 16target: std.Build.ResolvedTarget,
 17optimize: std.builtin.OptimizeMode,
 18output_file: std.Build.GeneratedFile,
 19link_libc: bool,
 20use_clang: bool,
 21
 22pub const Options = struct {
 23    root_source_file: std.Build.LazyPath,
 24    target: std.Build.ResolvedTarget,
 25    optimize: std.builtin.OptimizeMode,
 26    link_libc: bool = true,
 27    use_clang: bool = true,
 28};
 29
 30pub fn create(owner: *std.Build, options: Options) *TranslateC {
 31    const translate_c = owner.allocator.create(TranslateC) catch @panic("OOM");
 32    const source = options.root_source_file.dupe(owner);
 33    translate_c.* = .{
 34        .step = Step.init(.{
 35            .id = base_id,
 36            .name = "translate-c",
 37            .owner = owner,
 38            .makeFn = make,
 39        }),
 40        .source = source,
 41        .include_dirs = std.array_list.Managed(std.Build.Module.IncludeDir).init(owner.allocator),
 42        .c_macros = std.array_list.Managed([]const u8).init(owner.allocator),
 43        .out_basename = undefined,
 44        .target = options.target,
 45        .optimize = options.optimize,
 46        .output_file = .{ .step = &translate_c.step },
 47        .link_libc = options.link_libc,
 48        .use_clang = options.use_clang,
 49    };
 50    source.addStepDependencies(&translate_c.step);
 51    return translate_c;
 52}
 53
 54pub const AddExecutableOptions = struct {
 55    name: ?[]const u8 = null,
 56    version: ?std.SemanticVersion = null,
 57    target: ?std.Build.ResolvedTarget = null,
 58    optimize: ?std.builtin.OptimizeMode = null,
 59    linkage: ?std.builtin.LinkMode = null,
 60};
 61
 62pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
 63    return .{ .generated = .{ .file = &translate_c.output_file } };
 64}
 65
 66/// Creates a module from the translated source and adds it to the package's
 67/// module set making it available to other packages which depend on this one.
 68/// `createModule` can be used instead to create a private module.
 69pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module {
 70    return translate_c.step.owner.addModule(name, .{
 71        .root_source_file = translate_c.getOutput(),
 72        .target = translate_c.target,
 73        .optimize = translate_c.optimize,
 74        .link_libc = translate_c.link_libc,
 75    });
 76}
 77
 78/// Creates a private module from the translated source to be used by the
 79/// current package, but not exposed to other packages depending on this one.
 80/// `addModule` can be used instead to create a public module.
 81pub fn createModule(translate_c: *TranslateC) *std.Build.Module {
 82    return translate_c.step.owner.createModule(.{
 83        .root_source_file = translate_c.getOutput(),
 84        .target = translate_c.target,
 85        .optimize = translate_c.optimize,
 86        .link_libc = translate_c.link_libc,
 87    });
 88}
 89
 90pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
 91    const b = translate_c.step.owner;
 92    translate_c.include_dirs.append(.{ .path_after = lazy_path.dupe(b) }) catch
 93        @panic("OOM");
 94    lazy_path.addStepDependencies(&translate_c.step);
 95}
 96
 97pub fn addSystemIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
 98    const b = translate_c.step.owner;
 99    translate_c.include_dirs.append(.{ .path_system = lazy_path.dupe(b) }) catch
100        @panic("OOM");
101    lazy_path.addStepDependencies(&translate_c.step);
102}
103
104pub fn addIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
105    const b = translate_c.step.owner;
106    translate_c.include_dirs.append(.{ .path = lazy_path.dupe(b) }) catch
107        @panic("OOM");
108    lazy_path.addStepDependencies(&translate_c.step);
109}
110
111pub fn addConfigHeader(translate_c: *TranslateC, config_header: *Step.ConfigHeader) void {
112    translate_c.include_dirs.append(.{ .config_header_step = config_header }) catch
113        @panic("OOM");
114    translate_c.step.dependOn(&config_header.step);
115}
116
117pub fn addSystemFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
118    const b = translate_c.step.owner;
119    translate_c.include_dirs.append(.{ .framework_path_system = directory_path.dupe(b) }) catch
120        @panic("OOM");
121    directory_path.addStepDependencies(&translate_c.step);
122}
123
124pub fn addFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
125    const b = translate_c.step.owner;
126    translate_c.include_dirs.append(.{ .framework_path = directory_path.dupe(b) }) catch
127        @panic("OOM");
128    directory_path.addStepDependencies(&translate_c.step);
129}
130
131pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const u8) *Step.CheckFile {
132    return Step.CheckFile.create(
133        translate_c.step.owner,
134        translate_c.getOutput(),
135        .{ .expected_matches = expected_matches },
136    );
137}
138
139/// If the value is omitted, it is set to 1.
140/// `name` and `value` need not live longer than the function call.
141pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {
142    const macro = translate_c.step.owner.fmt("{s}={s}", .{ name, value orelse "1" });
143    translate_c.c_macros.append(macro) catch @panic("OOM");
144}
145
146/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
147pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {
148    translate_c.c_macros.append(translate_c.step.owner.dupe(name_and_value)) catch @panic("OOM");
149}
150
151fn make(step: *Step, options: Step.MakeOptions) !void {
152    const prog_node = options.progress_node;
153    const b = step.owner;
154    const translate_c: *TranslateC = @fieldParentPtr("step", step);
155
156    var argv_list = std.array_list.Managed([]const u8).init(b.allocator);
157    try argv_list.append(b.graph.zig_exe);
158    try argv_list.append("translate-c");
159    if (translate_c.link_libc) {
160        try argv_list.append("-lc");
161    }
162    if (!translate_c.use_clang) {
163        try argv_list.append("-fno-clang");
164    }
165
166    try argv_list.append("--cache-dir");
167    try argv_list.append(b.cache_root.path orelse ".");
168
169    try argv_list.append("--global-cache-dir");
170    try argv_list.append(b.graph.global_cache_root.path orelse ".");
171
172    try argv_list.append("--listen=-");
173
174    if (!translate_c.target.query.isNative()) {
175        try argv_list.append("-target");
176        try argv_list.append(try translate_c.target.query.zigTriple(b.allocator));
177    }
178
179    switch (translate_c.optimize) {
180        .Debug => {}, // Skip since it's the default.
181        else => try argv_list.append(b.fmt("-O{s}", .{@tagName(translate_c.optimize)})),
182    }
183
184    for (translate_c.include_dirs.items) |include_dir| {
185        try include_dir.appendZigProcessFlags(b, &argv_list, step);
186    }
187
188    for (translate_c.c_macros.items) |c_macro| {
189        try argv_list.append("-D");
190        try argv_list.append(c_macro);
191    }
192
193    const c_source_path = translate_c.source.getPath2(b, step);
194    try argv_list.append(c_source_path);
195
196    const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa);
197
198    const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));
199    translate_c.out_basename = b.fmt("{s}.zig", .{basename});
200    translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM");
201}