This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
110 lines (94 loc) · 4.91 KB
/
build.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
const std = @import("std");
const Move = @import("build/move.zig").MoveFileStep;
const Repl = @import("build/replace.zig").ReplaceInFileStep;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const no_bison = b.option(bool, "no-bison", "Do not run bison") orelse false;
const no_flex = b.option(bool, "no-flex", "Do not run flex") orelse false;
const no_gen = b.option(bool, "no-gen", "Do not run flex nor bison") orelse false;
const debug_bison = b.option(bool, "debug-bison", "Run bison with the `-t` flag") orelse false;
const debug_flex = b.option(bool, "debug-flex", "Run flex with the `-d` flag") orelse false;
const debug_gen = b.option(bool, "debug-gen", "Run flex and bison with debug flags") orelse false;
const table = b.option(bool, "table", "Print symbol table") orelse false;
const tree = b.option(bool, "tree", "Print basic AST") orelse false;
const werror = b.option(bool, "error", "Treat warnings as errors") orelse false;
_ = std.fs.cwd().openFile("build.zig", .{}) catch {
// Change current directory to where `build.zig` is.
const build_path = b.build_root.path.?;
std.log.info("Changing current working directory to: {s}", .{build_path});
std.process.changeCurDir(build_path) catch {
std.log.err("Failed to set cwd to {s}", .{build_path});
return;
};
};
// Gen bison & flex files
const mv_flex = Move.create(b, &.{"./lex.yy.c"}, &.{"./src/lexer.c"});
if (debug_gen or debug_flex) {
mv_flex.step.dependOn(&b.addSystemCommand(&.{ "flex", "-d", "src/lexer.l" }).step);
} else {
mv_flex.step.dependOn(&b.addSystemCommand(&.{ "flex", "src/lexer.l" }).step);
}
const mv_bison = Move.create(b, &.{ "./grammar.tab.c", "./grammar.tab.h" }, &.{ "./src/parser.c", "./src/parser.h" });
mv_bison.step.dependOn(&b.addSystemCommand(&.{ "bison", if (debug_gen or debug_bison) "-dt" else "-d", "src/grammar.y" }).step);
const gen_step = b.step("gen", "Generar lexer.{c,h}, parser.{c, h}");
gen_step.dependOn(&mv_flex.step);
gen_step.dependOn(&mv_bison.step);
const flags = .{ "-Wall", "-Wextra", "-pedantic", "-Wno-missing-braces", "-Wmissing-field-initializers", if (werror) "-Werror" else "", if (werror) "-pedantic-errors" else "", if (target.result.isMinGW()) "-DWIN" else "", if (tree) "-DPRINT_TREE" else "", if (table) "-DPRINT_TABLE" else "" };
// The main source code files without `main.c`. It is easier to compile it with specific main
// files so that, for example, we can test it.
const lnglib = b.addStaticLibrary(.{
.name = "lnglib",
.target = target,
.optimize = optimize,
});
lnglib.linkLibC();
lnglib.addCSourceFiles(.{ .files = &.{ "src/str.c", "src/vector.c", "src/hashset.c", "src/parser.c", "src/lexer.c", "src/symbol.c", "src/tree.c", "src/node.c", "src/panic.c" }, .flags = &flags });
if (!no_flex and !no_gen) lnglib.step.dependOn(&mv_flex.step);
if (!no_bison and !no_gen) {
if (debug_bison or debug_gen) {
const en_debug = Repl.create(b, &.{"src/parser.c"}, &.{ "int yydebug;", "grammar.tab.h" }, &.{ "int yydebug = 1;", "parser.h" });
const en_debug2 = Repl.create(b, &.{"src/parser.c"}, &.{"extern int yydebug = 1;"}, &.{"extern int yydebug;"});
en_debug.step.dependOn(&mv_bison.step);
en_debug2.step.dependOn(&en_debug.step);
lnglib.step.dependOn(&en_debug2.step);
} else {
const repl = Repl.create(b, &.{ "src/parser.c", "src/parser.h" }, &.{"grammar.tab.h"}, &.{"parser.h"});
repl.step.dependOn(&mv_bison.step);
lnglib.step.dependOn(&repl.step);
}
}
{
const lng = b.addExecutable(.{
.name = "lng",
.target = target,
.optimize = optimize,
});
b.installArtifact(lng);
lng.linkLibC();
lng.linkLibrary(lnglib);
lng.addCSourceFiles(.{ .files = &.{"src/main.c"}, .flags = &flags });
const run_cmd = b.addRunArtifact(lng);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
{
const lng = b.addExecutable(.{
.name = "test",
.target = target,
.optimize = optimize,
});
b.installArtifact(lng);
lng.linkLibC();
lng.linkLibrary(lnglib);
lng.addCSourceFiles(.{ .files = &.{ "test/main.c", "test/str_test.c", "test/vec_test.c", "test/hash_test.c", "test/tree_test.c" }, .flags = &.{} });
const run_cmd = b.addRunArtifact(lng);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("test", "Run tests");
run_step.dependOn(&run_cmd.step);
}
}