forked from ryupold/raylib.zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.zig
529 lines (453 loc) · 16.9 KB
/
generate.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
const std = @import("std");
const fs = std.fs;
const json = std.json;
const allocPrint = std.fmt.allocPrint;
const mapping = @import("type_mapping.zig");
const intermediate = @import("intermediate.zig");
pub const outputFile = "raylib.zig";
pub const injectFile = "inject.zig";
fn trim(s: []const u8) []const u8 {
return std.mem.trim(u8, s, &[_]u8{ ' ', '\t', '\n' });
}
pub fn main() !void {
std.log.info("generating raylib.zig ...", .{});
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit() == .leak) {
std.log.err("memory leak detected", .{});
}
}
const allocator = gpa.allocator();
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const bindingsData = try fs.cwd().readFileAlloc(arena.allocator(), intermediate.bindingsJSON, std.math.maxInt(usize));
const bindings = try json.parseFromSliceLeaky(mapping.Intermediate, arena.allocator(), bindingsData, .{
.ignore_unknown_fields = true,
});
var file = try fs.cwd().createFile(outputFile, .{});
defer file.close();
try file.writeAll("\n\n");
const inject = try Injections.load(arena.allocator());
try writeInjections(arena.allocator(), &file, inject);
try writeFunctions(arena.allocator(), &file, bindings, inject);
var h = try fs.cwd().createFile("marshal.h", .{});
defer h.close();
var c = try fs.cwd().createFile("marshal.c", .{});
defer c.close();
const raylib: mapping.CombinedRaylib = try mapping.CombinedRaylib.load(arena.allocator(), intermediate.jsonFiles);
try writeCFunctions(arena.allocator(), &h, &c, inject, raylib);
try writeStructs(arena.allocator(), &file, bindings, inject);
try writeEnums(arena.allocator(), &file, bindings, inject);
try writeDefines(arena.allocator(), &file, bindings, inject);
std.log.info("... done", .{});
}
const Injections = struct {
lines: []const []const u8,
symbols: []const []const u8,
pub fn load(allocator: std.mem.Allocator) !@This() {
var injectZigLines = std.ArrayList([]const u8).init(allocator);
var symbols = std.ArrayList([]const u8).init(allocator);
var file = try fs.cwd().openFile(injectFile, .{});
var reader = file.reader();
while (try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', std.math.maxInt(usize))) |line| {
if (std.mem.indexOf(u8, line, "pub const ")) |startIndex| {
if (std.mem.indexOf(u8, line, " = extern struct {")) |endIndex| {
const s = line["pub const ".len + startIndex .. endIndex];
try symbols.append(s);
std.log.debug("inject symbol: {s}", .{s});
} else if (std.mem.indexOf(u8, line, " = packed struct(u32) {")) |endIndex| {
const s = line["pub const ".len + startIndex .. endIndex];
try symbols.append(s);
std.log.debug("inject symbol: {s}", .{s});
}
}
if (std.mem.indexOf(u8, line, "pub fn ")) |startIndex| {
if (std.mem.indexOf(u8, line, "(")) |endIndex| {
const s = line["pub fn ".len + startIndex .. endIndex];
try symbols.append(s);
std.log.debug("inject symbol: {s}", .{s});
}
}
try injectZigLines.append(line);
}
return @This(){
.lines = try injectZigLines.toOwnedSlice(),
.symbols = try symbols.toOwnedSlice(),
};
}
pub fn containsSymbol(self: @This(), name: []const u8) bool {
for (self.symbols) |s| {
if (eql(s, name)) return true;
}
return false;
}
};
fn writeFunctions(
allocator: std.mem.Allocator,
file: *fs.File,
bindings: mapping.Intermediate,
inject: Injections,
) !void {
var buf: [51200]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
for (bindings.functions) |func| {
if (inject.containsSymbol(func.name)) continue;
defer fba.reset();
//--- signature -------------------------
const funcDescription: []const u8 = func.description orelse "";
try file.writeAll(
try allocPrint(
allocator,
"\n/// {s}\npub fn {s} (\n",
.{ funcDescription, func.name },
),
);
for (func.params) |param| {
if (param.description) |description| {
try file.writeAll(try allocPrint(
allocator,
"/// {s}\n",
.{description},
));
}
try file.writeAll(try allocPrint(
allocator,
"{s}: {s},\n",
.{ param.name, param.typ },
));
}
if (func.custom) {
try file.writeAll(
try allocPrint(
allocator,
") {s} {{\n",
.{func.returnType},
),
);
} else if (isPointer(func.returnType)) {
if (eql("u8", (stripType(func.returnType)))) {
try file.writeAll(
try allocPrint(
allocator,
") [*:0]const {s} {{\n",
.{stripType(func.returnType)},
),
);
} else {
try file.writeAll(
try allocPrint(
allocator,
") {s} {{\n",
.{func.returnType},
),
);
}
} else {
try file.writeAll(
try allocPrint(
allocator,
") {s} {{\n",
.{func.returnType},
),
);
}
const returnTypeIsVoid = eql(func.returnType, "void");
//--- body ------------------------------
if (isPointer(func.returnType)) {
try file.writeAll(try allocPrint(allocator, "return @as({s},@ptrCast(\n", .{func.returnType}));
} else if (isPrimitiveOrPointer(func.returnType)) {
try file.writeAll("return ");
} else if (!returnTypeIsVoid) {
try file.writeAll(try allocPrint(allocator, "var out: {s} = undefined;\n", .{func.returnType}));
}
try file.writeAll(try allocPrint(allocator, "raylib.m{s}(\n", .{func.name}));
if (!isPrimitiveOrPointer(func.returnType)) {
if (bindings.containsStruct(stripType(func.returnType))) {
try file.writeAll(try allocPrint(allocator, "@as([*c]raylib.{s}, @ptrCast(&out)),\n", .{func.returnType}));
} else if (!returnTypeIsVoid) {
try file.writeAll(try allocPrint(allocator, "@as([*c]{s}, @ptrCast(&out)),\n", .{func.returnType}));
}
}
for (func.params) |param| {
if (isFunctionPointer(param.typ)) {
try file.writeAll(try allocPrint(allocator, "@ptrCast({s}),\n", .{param.name}));
} else if (bindings.containsStruct(stripType(param.typ)) and isPointer(param.typ)) {
try file.writeAll(try allocPrint(allocator, "@as([*c]raylib.{s}, @ptrFromInt(@intFromPtr({s}))),\n", .{ stripType(param.typ), param.name }));
} else if (bindings.containsEnum(param.typ)) {
try file.writeAll(try allocPrint(allocator, "@intFromEnum({s}),\n", .{param.name}));
} else if (bindings.containsStruct(stripType(param.typ))) {
try file.writeAll(try allocPrint(allocator, "@as([*c]raylib.{s}, @ptrFromInt(@intFromPtr(&{s}))),\n", .{ stripType(param.typ), param.name }));
} else if (isPointer(param.typ)) {
if (std.mem.endsWith(u8, param.typ, "anyopaque")) {
try file.writeAll(try allocPrint(allocator, "{s},\n", .{param.name}));
} else if (isConst(param.typ)) {
try file.writeAll(try allocPrint(allocator, "@as([*c]const {s}, @ptrFromInt(@intFromPtr({s}))),\n", .{ stripType(param.typ), param.name }));
} else {
try file.writeAll(try allocPrint(allocator, "@as([*c]{s}, @ptrCast({s})),\n", .{ stripType(param.typ), param.name }));
}
} else {
try file.writeAll(try allocPrint(allocator, "{s},\n", .{param.name}));
}
}
if (isPointer(func.returnType)) {
try file.writeAll("),\n));\n");
} else {
try file.writeAll(");\n");
}
if (!isPrimitiveOrPointer(func.returnType) and !returnTypeIsVoid) {
try file.writeAll("return out;\n");
}
try file.writeAll("}\n");
}
std.log.info("generated functions", .{});
}
/// write: RETURN NAME(PARAMS...)
/// or: void NAME(RETURN*, PARAMS...)
fn writeCSignature(
allocator: std.mem.Allocator,
file: *fs.File,
func: mapping.RaylibFunction,
) !void {
const returnType = func.returnType;
//return directly
if (mapping.isPrimitiveOrPointer(returnType)) {
try file.writeAll(try allocPrint(allocator, "{s} m{s}(", .{ returnType, func.name }));
if (func.params == null or func.params.?.len == 0) {
try file.writeAll("void");
}
}
//wrap return type and put as first function parameter
else {
try file.writeAll(try allocPrint(allocator, "void m{s}({s} *out", .{ func.name, returnType }));
if (func.params != null and func.params.?.len > 0) {
try file.writeAll(", ");
}
}
if (func.params) |params| {
for (params, 0..) |param, i| {
const paramType = param.type;
if (mapping.isPrimitiveOrPointer(paramType) or isFunctionPointer(paramType)) {
try file.writeAll(try allocPrint(allocator, "{s} {s}", .{ paramType, param.name }));
} else {
try file.writeAll(try allocPrint(allocator, "{s} *{s}", .{ paramType, param.name }));
}
if (i < params.len - 1) {
try file.writeAll(", ");
}
}
}
try file.writeAll(")");
}
fn writeCFunctions(
allocator: std.mem.Allocator,
h: *fs.File,
c: *fs.File,
inject: Injections,
rl: mapping.CombinedRaylib,
) !void {
var hInject = try fs.cwd().openFile("inject.h", .{});
defer hInject.close();
try h.writeFileAll(hInject, .{});
var cInject = try fs.cwd().openFile("inject.c", .{});
defer cInject.close();
try c.writeFileAll(cInject, .{});
for (rl.functions.values()) |func| {
if (inject.containsSymbol(func.name)) continue;
//--- C-HEADER -----------------------------
try h.writeAll(try allocPrint(allocator, "// {s}\n", .{func.description}));
try writeCSignature(allocator, h, func);
try h.writeAll(";\n\n");
try writeCSignature(allocator, c, func);
try c.writeAll("\n{\n");
//--- C-IMPLEMENT -----------------------------
if (eql(func.returnType, "void")) {
try c.writeAll("\t");
} else if (mapping.isPrimitiveOrPointer(func.returnType)) {
try c.writeAll("\treturn ");
} else {
try c.writeAll("\t*out = ");
}
try c.writeAll(
try allocPrint(
allocator,
"{s}(",
.{func.name},
),
);
if (func.params) |params| {
for (params, 0..) |param, i| {
if (mapping.isPrimitiveOrPointer(param.type) or isFunctionPointer(param.type)) {
try c.writeAll(
try allocPrint(
allocator,
"{s}",
.{param.name},
),
);
} else {
try c.writeAll(
try allocPrint(
allocator,
"*{s}",
.{param.name},
),
);
}
if (i < params.len - 1) {
try c.writeAll(", ");
}
}
}
try c.writeAll(");\n}\n\n");
}
}
fn writeStructs(
allocator: std.mem.Allocator,
file: *fs.File,
bindings: mapping.Intermediate,
inject: Injections,
) !void {
var buf: [51200]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
for (bindings.structs) |s| {
if (inject.containsSymbol(s.name)) continue;
defer fba.reset();
try file.writeAll(
try allocPrint(
allocator,
"\n/// {?s}\npub const {s} = extern struct {{\n",
.{ s.description, s.name },
),
);
for (s.fields) |field| {
try file.writeAll(try allocPrint(allocator, "/// {?s}\n\t{s}: {s},\n", .{
field.description,
field.name,
field.typ,
}));
}
try file.writeAll("\n};\n");
}
std.log.info("generated structs", .{});
}
fn writeEnums(
allocator: std.mem.Allocator,
file: *fs.File,
bindings: mapping.Intermediate,
inject: Injections,
) !void {
var buf: [51200]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
for (bindings.enums) |e| {
if (inject.containsSymbol(e.name)) continue;
defer fba.reset();
try file.writeAll(
try allocPrint(
allocator,
"\n/// {?s}\npub const {s} = enum(i32) {{\n",
.{ e.description, e.name },
),
);
for (e.values) |value| {
try file.writeAll(try allocPrint(allocator, "/// {?s}\n{s} = {d},\n", .{
value.description,
value.name,
value.value,
}));
}
try file.writeAll("\n};\n");
}
std.log.info("generated enums", .{});
}
fn writeDefines(
allocator: std.mem.Allocator,
file: *fs.File,
bindings: mapping.Intermediate,
inject: Injections,
) !void {
var buf: [51200]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
for (bindings.defines) |d| {
if (inject.containsSymbol(d.name)) continue;
defer fba.reset();
try file.writeAll(
try allocPrint(
allocator,
"\n/// {?s}\npub const {s}: {s} = {s};\n",
.{ d.description, d.name, d.typ, d.value },
),
);
}
std.log.info("generated defines", .{});
}
fn writeInjections(
_: std.mem.Allocator,
file: *fs.File,
inject: Injections,
) !void {
var buf: [51200]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
for (inject.lines) |line| {
defer fba.reset();
try file.writeAll(try allocPrint(fba.allocator(), "{s}\n", .{line}));
}
std.log.info("written inject.zig", .{});
}
fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
fn startsWith(haystack: []const u8, needle: []const u8) bool {
return std.mem.startsWith(u8, haystack, needle);
}
fn endsWith(haystack: []const u8, needle: []const u8) bool {
return std.mem.endsWith(u8, haystack, needle);
}
/// is pointer type
fn isPointer(z: []const u8) bool {
return pointerOffset(z) > 0;
}
fn isFunctionPointer(z: []const u8) bool {
return std.mem.indexOf(u8, z, "fn(") != null or std.mem.endsWith(u8, z, "Callback");
}
fn pointerOffset(z: []const u8) usize {
if (startsWith(z, "*")) return 1;
if (startsWith(z, "?*")) return 2;
if (startsWith(z, "[*]")) return 3;
if (startsWith(z, "?[*]")) return 4;
if (startsWith(z, "[*c]")) return 4;
if (startsWith(z, "[*:0]")) return 5;
if (startsWith(z, "?[*:0]")) return 6;
return 0;
}
fn isConst(z: []const u8) bool {
return startsWith(z[pointerOffset(z)..], "const ");
}
fn isVoid(z: []const u8) bool {
return eql(stripType(z), "void");
}
/// strips const and pointer annotations
/// *const TName -> TName
fn stripType(z: []const u8) []const u8 {
var name = z[pointerOffset(z)..];
name = if (startsWith(name, "const ")) name["const ".len..] else name;
return std.mem.trim(u8, name, " \t\n");
}
/// true if Zig type is primitive or a pointer to anything
/// this means we don't need to wrap it in a pointer
pub fn isPrimitiveOrPointer(z: []const u8) bool {
const primitiveTypes = std.ComptimeStringMap(void, .{
// .{ "void", {} }, // zig void is zero sized while C void is >= 1 byte
.{ "bool", {} },
.{ "u8", {} },
.{ "i8", {} },
.{ "i16", {} },
.{ "u16", {} },
.{ "i32", {} },
.{ "u32", {} },
.{ "i64", {} },
.{ "u64", {} },
.{ "f32", {} },
.{ "f64", {} },
});
return primitiveTypes.has(stripType(z)) or pointerOffset(z) > 0;
}