Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
155 lines (139 loc) · 5.73 KB
/
Copy pathbuild.zig
File metadata and controls
155 lines (139 loc) · 5.73 KB
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
const std = @import("std");
const thp_version: std.SemanticVersion = .{
.major = 0,
.minor = 0,
.patch = 4,
};
fn create_module(
path: []const u8,
b: *std.Build,
t: std.Build.ResolvedTarget,
o: std.builtin.OptimizeMode,
) *std.Build.Module {
return b.createModule(.{
.root_source_file = b.path(path),
.target = t,
.optimize = o,
});
}
fn main_executable(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
options_module: *std.Build.Module,
no_bin: bool,
) void {
const thpvm_dep = b.dependency("thpvm", .{
.target = target,
.optimize = optimize,
});
const thpvm_module = thpvm_dep.module("thpvm");
//
// Modules
//
const cli_module = create_module("src/cli/root.zig", b, target, optimize);
const error_module = create_module("src/error_context/root.zig", b, target, optimize);
const lexic_module = create_module("src/01_lexic/root.zig", b, target, optimize);
const syntax_module = create_module("src/02_syntax/root.zig", b, target, optimize);
const semantic_module = create_module("src/03_semantic/root.zig", b, target, optimize);
const codegen_module = create_module("src/04_codegen/root.zig", b, target, optimize);
const root_module = create_module("src/main.zig", b, target, optimize);
//
// set up module dependencies
//
cli_module.addImport("config", options_module);
cli_module.addImport("vm", thpvm_module);
//
error_module.addImport("config", options_module);
//
lexic_module.addImport("config", options_module);
lexic_module.addImport("context", error_module);
//
syntax_module.addImport("config", options_module);
syntax_module.addImport("context", error_module);
syntax_module.addImport("lexic", lexic_module);
// circular dependency on semantic: syntax needs to know
// about the semantic visitors, and semantic needs to know
// about the syntax AST
syntax_module.addImport("semantic", semantic_module);
//
semantic_module.addImport("config", options_module);
semantic_module.addImport("context", error_module);
semantic_module.addImport("lexic", lexic_module);
semantic_module.addImport("syntax", syntax_module);
//
codegen_module.addImport("config", options_module);
codegen_module.addImport("context", error_module);
codegen_module.addImport("lexic", lexic_module);
codegen_module.addImport("syntax", syntax_module);
codegen_module.addImport("semantic", semantic_module);
codegen_module.addImport("vm", thpvm_module);
//
root_module.addImport("config", options_module);
root_module.addImport("context", error_module);
root_module.addImport("lexic", lexic_module);
root_module.addImport("syntax", syntax_module);
root_module.addImport("semantic", semantic_module);
root_module.addImport("codegen", codegen_module);
root_module.addImport("vm", thpvm_module);
// ==============================
//
// Main executable
//
// ==============================
const exe = b.addExecutable(.{
.name = "thp",
.root_module = root_module,
});
// If using -Dno-bin, use the x86-backend for fast builds
if (no_bin) {
exe.use_llvm = false;
b.getInstallStep().dependOn(&exe.step);
return;
} else {
b.installArtifact(exe);
}
// Run command
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
// Pass arguments if any
if (b.args) |args| {
run_cmd.addArgs(args);
}
// Run step
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
//
// Unit tests
//
const cli_module_tests = b.addTest(.{ .name = "cli_module", .root_module = cli_module });
const error_module_tests = b.addTest(.{ .name = "error_module", .root_module = error_module });
const lexic_module_tests = b.addTest(.{ .name = "lexic", .root_module = lexic_module });
const syntax_module_tests = b.addTest(.{ .name = "syntax", .root_module = syntax_module });
const semantic_module_tests = b.addTest(.{ .name = "semantic", .root_module = semantic_module });
const codegen_module_tests = b.addTest(.{ .name = "codegen", .root_module = codegen_module });
const root_module_tests = b.addTest(.{ .name = "root", .root_module = root_module });
const test_step = b.step("test", "Run all unit tests");
test_step.dependOn(&b.addRunArtifact(cli_module_tests).step);
test_step.dependOn(&b.addRunArtifact(error_module_tests).step);
test_step.dependOn(&b.addRunArtifact(lexic_module_tests).step);
test_step.dependOn(&b.addRunArtifact(syntax_module_tests).step);
test_step.dependOn(&b.addRunArtifact(semantic_module_tests).step);
test_step.dependOn(&b.addRunArtifact(codegen_module_tests).step);
test_step.dependOn(&b.addRunArtifact(root_module_tests).step);
}
pub fn build(b: *std.Build) void {
// Standard target options
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Create options module for conditional compilation
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
const json_serialization = b.option(bool, "json", "enable JSON serialization of the compiler outputs") orelse false;
const no_bin = b.option(bool, "no-bin", "skip emitting binary") orelse false;
const options = b.addOptions();
options.addOption(bool, "tracing", executionTracing);
options.addOption(bool, "json", json_serialization);
// Create the options module that will be shared
const options_module = options.createModule();
main_executable(b, target, optimize, options_module, no_bin);
}