mirror of
https://github.com/ziglang/zig.git
synced 2024-11-16 17:15:37 +00:00
std.build.TranslateCStep: add C macro support
The string construction code is moved out of std.build.LibExeObjStep into std.build.constructCMacroArg, to allow reusing it elsewhere.
This commit is contained in:
parent
7d04ab1f14
commit
fbc06f9c91
@ -1862,15 +1862,7 @@ pub const LibExeObjStep = struct {
|
||||
/// If the value is omitted, it is set to 1.
|
||||
/// `name` and `value` need not live longer than the function call.
|
||||
pub fn defineCMacro(self: *LibExeObjStep, name: []const u8, value: ?[]const u8) void {
|
||||
var macro = self.builder.allocator.alloc(
|
||||
u8,
|
||||
name.len + if (value) |value_slice| value_slice.len + 1 else 0,
|
||||
) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
|
||||
mem.copy(u8, macro, name);
|
||||
if (value) |value_slice| {
|
||||
macro[name.len] = '=';
|
||||
mem.copy(u8, macro[name.len + 1 ..], value_slice);
|
||||
}
|
||||
const macro = constructCMacro(self.builder.allocator, name, value);
|
||||
self.c_macros.append(macro) catch unreachable;
|
||||
}
|
||||
|
||||
@ -2934,6 +2926,22 @@ pub const LibExeObjStep = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Allocates a new string for assigning a value to a named macro.
|
||||
/// If the value is omitted, it is set to 1.
|
||||
/// `name` and `value` need not live longer than the function call.
|
||||
pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u8) []const u8 {
|
||||
var macro = allocator.alloc(
|
||||
u8,
|
||||
name.len + if (value) |value_slice| value_slice.len + 1 else 0,
|
||||
) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
|
||||
mem.copy(u8, macro, name);
|
||||
if (value) |value_slice| {
|
||||
macro[name.len] = '=';
|
||||
mem.copy(u8, macro[name.len + 1 ..], value_slice);
|
||||
}
|
||||
return macro;
|
||||
}
|
||||
|
||||
pub const InstallArtifactStep = struct {
|
||||
pub const base_id = .install_artifact;
|
||||
|
||||
|
@ -16,6 +16,7 @@ step: Step,
|
||||
builder: *Builder,
|
||||
source: build.FileSource,
|
||||
include_dirs: std.ArrayList([]const u8),
|
||||
c_macros: std.ArrayList([]const u8),
|
||||
output_dir: ?[]const u8,
|
||||
out_basename: []const u8,
|
||||
target: CrossTarget = CrossTarget{},
|
||||
@ -28,6 +29,7 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
|
||||
.builder = builder,
|
||||
.source = source,
|
||||
.include_dirs = std.ArrayList([]const u8).init(builder.allocator),
|
||||
.c_macros = std.ArrayList([]const u8).init(builder.allocator),
|
||||
.output_dir = null,
|
||||
.out_basename = undefined,
|
||||
.output_file = build.GeneratedFile{ .step = &self.step },
|
||||
@ -53,6 +55,18 @@ pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8)
|
||||
return CheckFileStep.create(self.builder, .{ .generated = &self.output_file }, self.builder.dupeStrings(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(self: *TranslateCStep, name: []const u8, value: ?[]const u8) void {
|
||||
const macro = build.constructCMacro(self.builder.allocator, name, value);
|
||||
self.c_macros.append(macro) catch unreachable;
|
||||
}
|
||||
|
||||
/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
|
||||
pub fn defineCMacroRaw(self: *TranslateCStep, name_and_value: []const u8) void {
|
||||
self.c_macros.append(self.builder.dupe(name_and_value)) catch unreachable;
|
||||
}
|
||||
|
||||
fn make(step: *Step) !void {
|
||||
const self = @fieldParentPtr(TranslateCStep, "step", step);
|
||||
|
||||
@ -73,6 +87,11 @@ fn make(step: *Step) !void {
|
||||
try argv_list.append(include_dir);
|
||||
}
|
||||
|
||||
for (self.c_macros.items) |c_macro| {
|
||||
try argv_list.append("-D");
|
||||
try argv_list.append(c_macro);
|
||||
}
|
||||
|
||||
try argv_list.append(self.source.getPath(self.builder));
|
||||
|
||||
const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step);
|
||||
|
Loading…
Reference in New Issue
Block a user