Skip to content

std.Build.Step.TranslateC

Standard libraryZig 0.17.1Official documentation
Sourcestd/Build/Step/TranslateC.zig

Source line 1

Fields

step: Step
source: std.Build.LazyPath
include_dirs: std.ArrayList(std.Build.Module.IncludeDir) = .empty
system_libs: std.ArrayList(std.Build.Module.SystemLib) = .empty
cc_argv: std.ArrayList(Configuration.String) = .empty
target: std.Build.ResolvedTarget
optimize: std.builtin.Optimize
output_file: Configuration.GeneratedFileIndex
link_libc: bool

std.Build.Step.TranslateC.addAfterIncludePath

function

Source line 98

pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void

Parameters

translate_c: *TranslateC
lazy_path: LazyPath

std.Build.Step.TranslateC.addCFlags

function

Source line 171

Append arbitrary C compiler CLI arguments to the translation unit.

pub fn addCFlags(translate_c: *TranslateC, args: []const []const u8) void

Parameters

translate_c: *TranslateC
args: []const []const u8

std.Build.Step.TranslateC.addCheckFile

function

Source line 146

pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const u8) *Step.CheckFile

Parameters

translate_c: *TranslateC
expected_matches: []const []const u8

std.Build.Step.TranslateC.addConfigHeader

function

Source line 122

pub fn addConfigHeader(translate_c: *TranslateC, config_header: *Step.ConfigHeader) void

Parameters

translate_c: *TranslateC
config_header: *Step.ConfigHeader

std.Build.Step.TranslateC.AddExecutableOptions

container

Source line 49

Fields

name: ?[]const u8 = null
version: ?std.SemanticVersion = null
target: ?std.Build.ResolvedTarget = null
optimize: ?std.builtin.Optimize = null
linkage: ?std.builtin.LinkMode = null

std.Build.Step.TranslateC.addFrameworkPath

function

Source line 138

pub fn addFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void

Parameters

translate_c: *TranslateC
directory_path: LazyPath

std.Build.Step.TranslateC.addIncludePath

function

Source line 114

pub fn addIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void

Parameters

translate_c: *TranslateC
lazy_path: LazyPath

std.Build.Step.TranslateC.addModule

function

Source line 64

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(translate_c: *TranslateC, name: []const u8) *std.Build.Module

Parameters

translate_c: *TranslateC
name: []const u8

std.Build.Step.TranslateC.addSystemFrameworkPath

function

Source line 130

pub fn addSystemFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void

Parameters

translate_c: *TranslateC
directory_path: LazyPath

std.Build.Step.TranslateC.addSystemIncludePath

function

Source line 106

pub fn addSystemIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void

Parameters

translate_c: *TranslateC
lazy_path: LazyPath

std.Build.Step.TranslateC.base_tag

constant

Source line 20

Step.Tag

std.Build.Step.TranslateC.Configuration

alias

Source line 8

Alias of std.Build.Configuration

std.Build.Step.TranslateC.create

function

Source line 29

pub fn create(owner: *std.Build, options: Options) *TranslateC

Parameters

owner: *std.Build
options: Options

std.Build.Step.TranslateC.createModule

function

Source line 76

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(translate_c: *TranslateC) *std.Build.Module

Parameters

translate_c: *TranslateC

std.Build.Step.TranslateC.defineCMacro

function

Source line 156

If the value is omitted, it is set to 1. name and value need not live longer than the function call.

pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void

Parameters

translate_c: *TranslateC
name: []const u8
value: ?[]const u8

std.Build.Step.TranslateC.defineCMacroRaw

function

Source line 166

name_and_value looks like [name]=[value].

pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void

Parameters

translate_c: *TranslateC
name_and_value: []const u8

std.Build.Step.TranslateC.fs

alias

Source line 4

Alias of std.fs

std.Build.Step.TranslateC.getOutput

function

Source line 57

pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath

Parameters

translate_c: *TranslateC

std.Build.Step.TranslateC.LazyPath

alias

Source line 7

Alias of std.Build.LazyPath

std.Build.Step.TranslateC.linkSystemLibrary

function

Source line 179

pub fn linkSystemLibrary( translate_c: *TranslateC, name: []const u8, options: std.Build.Module.LinkSystemLibraryOptions, ) void

Parameters

translate_c: *TranslateC
name: []const u8
options: std.Build.Module.LinkSystemLibraryOptions

std.Build.Step.TranslateC.mem

alias

Source line 5

Alias of std.mem

std.Build.Step.TranslateC.Options

container

Source line 22

Fields

root_source_file: std.Build.LazyPath
target: std.Build.ResolvedTarget
optimize: std.builtin.Optimize
link_libc: bool = true

std.Build.Step.TranslateC.setUpModule

function

Source line 85

fn setUpModule(translate_c: *TranslateC, module: *std.Build.Module) *std.Build.Module

Parameters

translate_c: *TranslateC
module: *std.Build.Module

std.Build.Step.TranslateC.std

alias

Source line 3

Alias of std

std.Build.Step.TranslateC.Step

alias

Source line 6

Alias of std.Build.Step

std.Build.Step.TranslateC.TranslateC

alias

Source line 1

Alias of std.Build.Step.TranslateC

Source code

std/Build/Step/TranslateC.zig

const TranslateC = @This();

const std = @import("std");
const fs = std.fs;
const mem = std.mem;
const Step = std.Build.Step;
const LazyPath = std.Build.LazyPath;
const Configuration = std.Build.Configuration;

step: Step,
source: std.Build.LazyPath,
include_dirs: std.ArrayList(std.Build.Module.IncludeDir) = .empty,
system_libs: std.ArrayList(std.Build.Module.SystemLib) = .empty,
cc_argv: std.ArrayList(Configuration.String) = .empty,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Optimize,
output_file: Configuration.GeneratedFileIndex,
link_libc: bool,

pub const base_tag: Step.Tag = .translate_c;

pub const Options = struct {
    root_source_file: std.Build.LazyPath,
    target: std.Build.ResolvedTarget,
    optimize: std.builtin.Optimize,
    link_libc: bool = true,
};

pub fn create(owner: *std.Build, options: Options) *TranslateC {
    const graph = owner.graph;
    const translate_c = graph.create(TranslateC);
    const source = options.root_source_file.dupe(graph);
    translate_c.* = .{
        .step = .init(.{
            .tag = base_tag,
            .name = "translate-c",
            .owner = owner,
        }),
        .source = source,
        .target = options.target,
        .optimize = options.optimize,
        .output_file = graph.addGeneratedFile(&translate_c.step),
        .link_libc = options.link_libc,
    };
    source.addStepDependencies(&translate_c.step);
    return translate_c;
}

pub const AddExecutableOptions = struct {
    name: ?[]const u8 = null,
    version: ?std.SemanticVersion = null,
    target: ?std.Build.ResolvedTarget = null,
    optimize: ?std.builtin.Optimize = null,
    linkage: ?std.builtin.LinkMode = null,
};

pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
    return .{ .generated = .{ .index = translate_c.output_file } };
}

/// 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(translate_c: *TranslateC, name: []const u8) *std.Build.Module {
    return setUpModule(translate_c, translate_c.step.owner.addModule(name, .{
        .root_source_file = translate_c.getOutput(),
        .target = translate_c.target,
        .optimize = translate_c.optimize,
        .link_libc = translate_c.link_libc,
    }));
}

/// 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(translate_c: *TranslateC) *std.Build.Module {
    return setUpModule(translate_c, translate_c.step.owner.createModule(.{
        .root_source_file = translate_c.getOutput(),
        .target = translate_c.target,
        .optimize = translate_c.optimize,
        .link_libc = translate_c.link_libc,
    }));
}

fn setUpModule(translate_c: *TranslateC, module: *std.Build.Module) *std.Build.Module {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;

    if (translate_c.link_libc) module.link_libc = true;

    for (translate_c.system_libs.items) |system_lib| {
        module.link_objects.append(arena, .{ .system_lib = system_lib }) catch @panic("OOM");
    }

    return module;
}

pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .path_after = lazy_path.dupe(graph) }) catch
        @panic("OOM");
    lazy_path.addStepDependencies(&translate_c.step);
}

pub fn addSystemIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .path_system = lazy_path.dupe(graph) }) catch
        @panic("OOM");
    lazy_path.addStepDependencies(&translate_c.step);
}

pub fn addIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .path = lazy_path.dupe(graph) }) catch
        @panic("OOM");
    lazy_path.addStepDependencies(&translate_c.step);
}

pub fn addConfigHeader(translate_c: *TranslateC, config_header: *Step.ConfigHeader) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .config_header_step = config_header }) catch
        @panic("OOM");
    translate_c.step.dependOn(&config_header.step);
}

pub fn addSystemFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .framework_path_system = directory_path.dupe(graph) }) catch
        @panic("OOM");
    directory_path.addStepDependencies(&translate_c.step);
}

pub fn addFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.include_dirs.append(arena, .{ .framework_path = directory_path.dupe(graph) }) catch
        @panic("OOM");
    directory_path.addStepDependencies(&translate_c.step);
}

pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const u8) *Step.CheckFile {
    return Step.CheckFile.create(
        translate_c.step.owner,
        translate_c.getOutput(),
        .{ .expected_matches = expected_matches },
    );
}

/// If the value is omitted, it is set to 1.
/// `name` and `value` need not live longer than the function call.
pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    return addCFlags(translate_c, &.{
        "-D",
        arena.print("{s}={s}", .{ name, value orelse "1" }) catch @panic("OOM"),
    });
}

/// name_and_value looks like [name]=[value].
pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {
    return addCFlags(translate_c, &.{ "-D", name_and_value });
}

/// Append arbitrary C compiler CLI arguments to the translation unit.
pub fn addCFlags(translate_c: *TranslateC, args: []const []const u8) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    const wc = &graph.wip_configuration;
    translate_c.cc_argv.ensureUnusedCapacity(arena, args.len) catch @panic("OOM");
    for (args) |arg| translate_c.cc_argv.appendAssumeCapacity(wc.addString(arg) catch @panic("OOM"));
}

pub fn linkSystemLibrary(
    translate_c: *TranslateC,
    name: []const u8,
    options: std.Build.Module.LinkSystemLibraryOptions,
) void {
    const graph = translate_c.step.owner.graph;
    const arena = graph.arena;
    translate_c.system_libs.append(arena, .{
        .name = graph.dupeString(name),
        .needed = options.needed,
        .weak = options.weak,
        .use_pkg_config = options.use_pkg_config,
        .preferred_link_mode = options.preferred_link_mode,
        .search_strategy = options.search_strategy,
    }) catch @panic("OOM");
}