-
Notifications
You must be signed in to change notification settings - Fork 0
/
zingle_header.zig
129 lines (114 loc) · 4.53 KB
/
zingle_header.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const std = @import("std");
const Self = @This();
step: std.build.Step,
builder: *std.build.Builder,
source: []std.build.FileSource,
defines: [][]const u8,
output_file: std.build.GeneratedFile,
/// Allocates a ZingleHeaderStep, caller owns memory
pub fn create(builder: *std.build.Builder, files: [][]const u8, defines: [][]const u8) *Self {
var sources = builder.allocator.alloc(std.build.FileSource, files.len) catch unreachable;
for(files) |v,i| {
sources[i] = .{.path = builder.allocator.dupe(u8, v) catch unreachable};
}
return createSource(builder, sources, defines);
}
/// Allocates a ZingleHeaderStep, caller owns memory
pub fn createSource(builder: *std.build.Builder, sources: []std.build.FileSource, defines: [][]const u8) *Self {
const self = builder.allocator.create(Self) catch unreachable;
self.defines = builder.allocator.dupe([]const u8, defines) catch unreachable;
self.* = Self{
.step = std.build.Step.init(.custom, "single-header", builder.allocator, make),
.builder = builder,
.source = sources,
.defines = self.builder.dupeStrings(defines),
.output_file = std.build.GeneratedFile{ .step = &self.step },
};
for (sources) |source| {
source.addStepDependencies(&self.step);
}
return self;
}
pub fn getFileSource(self: *const Self) std.build.FileSource {
return std.build.FileSource{ .generated = &self.output_file };
}
fn make(step: *std.build.Step) !void {
const self = @fieldParentPtr(Self, "step", step);
const source_file_name = self.source[0].getPath(self.builder);
// std.debug.print("source = '{s}'\n", .{source_file_name});
const basename = std.fs.path.basename(source_file_name);
const output_name = blk: {
if (std.mem.indexOf(u8, basename, ".")) |index| {
break :blk try std.mem.join(self.builder.allocator, ".", &[_][]const u8{
basename[0..index],
"c",
});
} else {
break :blk try std.mem.join(self.builder.allocator, ".", &[_][]const u8{
basename,
"c",
});
}
};
var output_buffer = std.ArrayList(u8).init(self.builder.allocator);
defer output_buffer.deinit();
var writer = output_buffer.writer();
for (self.defines) |m| {
try writer.print("#define {s}\n", .{m});
}
for (self.source) |s| {
try writer.print("#include \"{s}\"\n", .{s.getPath(self.builder)});
}
var hash = std.crypto.hash.blake2.Blake2b384.init(.{});
hash.update("C9XVU4MxSDFZz2to");
hash.update(basename);
hash.update(output_buffer.items);
var digest: [48]u8 = undefined;
hash.final(&digest);
var hash_basename: [64]u8 = undefined;
_ = std.fs.base64_encoder.encode(&hash_basename, &digest);
const output_dir = try std.fs.path.join(self.builder.allocator, &[_][]const u8{
self.builder.cache_root,
"o",
&hash_basename,
});
var dir = std.fs.cwd().makeOpenPath(output_dir, .{}) catch |err| {
std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) });
return err;
};
self.output_file.path = try std.fs.path.join(self.builder.allocator, &[_][]const u8{
output_dir,
output_name,
});
dir.writeFile(output_name, output_buffer.items) catch |err| {
std.debug.print("unable to write {s} into {s}: {s}\n", .{
output_name,
output_dir,
@errorName(err),
});
return err;
};
}
/// Allocates a ZingleHeaderStep, caller owns memory
pub fn addSingleHeaderFiles(exeLibObj: *std.build.LibExeObjStep, paths: [][]const u8, defines: [][]const u8, flags: [][]const u8) *Self {
var zingle_step = create(exeLibObj.builder, paths, defines);
zingle_step.step.make() catch unreachable;
var cSource = std.build.CSourceFile{ .source = zingle_step.getFileSource(), .args = flags };
exeLibObj.step.dependOn(&zingle_step.step);
exeLibObj.addCSourceFileSource(cSource);
return zingle_step;
}
pub fn addSingleHeaderFile(exeLibObj: *std.build.LibExeObjStep, path: []const u8, defines: [][]const u8, flags: [][]const u8) *Self {
return addSingleHeaderFiles(exeLibObj,&.{path},defines,flags);
}
pub fn free(self: *const Self) void {
for (self.defines) |v| {
self.builder.allocator.free(v);
}
for(self.source) |v| {
self.builder.allocator.free(v.path);
}
self.builder.allocator.free(self.defines);
self.builder.allocator.free(self.source);
self.builder.allocator.destroy(self);
}