forked from zigtools/zls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
250 lines (215 loc) · 11.8 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
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
const std = @import("std");
const builtin = @import("builtin");
const zls_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0 };
/// document the latest breaking change that caused a change to the string below:
/// drop for loop syntax upgrade mechanisms
const min_zig_string = "0.12.0-dev.899+027aabf49";
pub fn build(b: *std.build.Builder) !void {
comptime {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
}
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const single_threaded = b.option(bool, "single-threaded", "Build a single threaded Executable");
const pie = b.option(bool, "pie", "Build a Position Independent Executable");
const enable_tracy = b.option(bool, "enable_tracy", "Whether tracy should be enabled.") orelse false;
const coverage = b.option(bool, "generate_coverage", "Generate coverage data with kcov") orelse false;
const coverage_output_dir = b.option([]const u8, "coverage_output_dir", "Output directory for coverage data") orelse b.pathJoin(&.{ b.install_prefix, "kcov" });
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
const data_version = b.option([]const u8, "data_version", "The Zig version your compiler is.") orelse "master";
const data_version_path = b.option([]const u8, "version_data_path", "Manually specify zig language reference file");
const override_version_data_file_path = b.option([]const u8, "version_data_file_path", "Relative path to version data file (if none, will be named with timestamp)");
const version_string = v: {
const version_string = b.fmt("{d}.{d}.{d}", .{ zls_version.major, zls_version.minor, zls_version.patch });
const build_root_path = b.build_root.path orelse ".";
var code: u8 = undefined;
const git_describe_untrimmed = b.execAllowFail(&[_][]const u8{
"git", "-C", build_root_path, "describe", "--match", "*.*.*", "--tags",
}, &code, .Ignore) catch break :v version_string;
const git_describe = std.mem.trim(u8, git_describe_untrimmed, " \n\r");
switch (std.mem.count(u8, git_describe, "-")) {
0 => {
// Tagged release version (e.g. 0.10.0).
std.debug.assert(std.mem.eql(u8, git_describe, version_string)); // tagged release must match version string
break :v version_string;
},
2 => {
// Untagged development build (e.g. 0.10.0-dev.216+34ce200).
var it = std.mem.splitScalar(u8, git_describe, '-');
const tagged_ancestor = it.first();
const commit_height = it.next().?;
const commit_id = it.next().?;
const ancestor_ver = try std.SemanticVersion.parse(tagged_ancestor);
std.debug.assert(zls_version.order(ancestor_ver) == .gt); // zls version must be greater than its previous version
std.debug.assert(std.mem.startsWith(u8, commit_id, "g")); // commit hash is prefixed with a 'g'
break :v b.fmt("{s}-dev.{s}+{s}", .{ version_string, commit_height, commit_id[1..] });
},
else => {
std.debug.print("Unexpected 'git describe' output: '{s}'\n", .{git_describe});
std.process.exit(1);
},
}
};
const exe_options = b.addOptions();
exe_options.addOption(std.log.Level, "log_level", b.option(std.log.Level, "log_level", "The Log Level to be used.") orelse .info);
exe_options.addOption(bool, "enable_tracy", enable_tracy);
exe_options.addOption(bool, "enable_tracy_allocation", b.option(bool, "enable_tracy_allocation", "Enable using TracyAllocator to monitor allocations.") orelse enable_tracy);
exe_options.addOption(bool, "enable_tracy_callstack", b.option(bool, "enable_tracy_callstack", "Enable callstack graphs.") orelse enable_tracy);
exe_options.addOption(bool, "enable_failing_allocator", b.option(bool, "enable_failing_allocator", "Whether to use a randomly failing allocator.") orelse false);
exe_options.addOption(u32, "enable_failing_allocator_likelihood", b.option(u32, "enable_failing_allocator_likelihood", "The chance that an allocation will fail is `1/likelihood`") orelse 256);
exe_options.addOption(bool, "use_gpa", b.option(bool, "use_gpa", "Good for debugging") orelse (optimize == .Debug));
exe_options.addOption([]const u8, "version_string", version_string);
exe_options.addOption(std.SemanticVersion, "version", try std.SemanticVersion.parse(version_string));
exe_options.addOption([]const u8, "min_zig_string", min_zig_string);
const build_options = b.addOptions();
const build_options_module = build_options.createModule();
build_options.addOption([]const u8, "version_string", version_string);
build_options.addOption(std.SemanticVersion, "version", try std.SemanticVersion.parse(version_string));
const global_cache_path = try b.cache_root.join(b.allocator, &.{"zls"});
b.cache_root.handle.makePath(global_cache_path) catch |err| {
std.debug.panic("unable to make tmp path '{s}': {}", .{ global_cache_path, err });
};
const test_options = b.addOptions();
const test_options_module = test_options.createModule();
test_options.addOption([]const u8, "zig_exe_path", b.zig_exe);
test_options.addOption([]const u8, "global_cache_path", global_cache_path);
const exe_options_module = exe_options.createModule();
const known_folders_module = b.dependency("known_folders", .{}).module("known-folders");
const diffz_module = b.dependency("diffz", .{}).module("diffz");
const binned_allocator_module = b.dependency("binned_allocator", .{}).module("binned_allocator");
const exe = b.addExecutable(.{
.name = "zls",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
.single_threaded = single_threaded,
});
exe.pie = pie;
b.installArtifact(exe);
exe.addModule("build_options", exe_options_module);
exe.addModule("known-folders", known_folders_module);
exe.addModule("diffz", diffz_module);
exe.addModule("binned_allocator", binned_allocator_module);
if (enable_tracy) {
const client_cpp = "src/tracy/public/TracyClient.cpp";
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
else
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
exe.addIncludePath(.{ .path = "src/tracy" });
exe.addCSourceFile(.{
.file = .{ .path = client_cpp },
.flags = tracy_c_flags,
});
exe.linkLibCpp();
exe.linkLibC();
if (target.isWindows()) {
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("ws2_32");
}
}
const gen_exe = b.addExecutable(.{
.name = "zls_gen",
.root_source_file = .{ .path = "src/config_gen/config_gen.zig" },
.single_threaded = true,
});
const gen_cmd = b.addRunArtifact(gen_exe);
gen_cmd.addArgs(&.{
"--readme-path",
b.pathFromRoot("README.md"),
"--generate-config-path",
b.pathFromRoot("src/Config.zig"),
"--generate-schema-path",
b.pathFromRoot("schema.json"),
});
if (b.args) |args| gen_cmd.addArgs(args);
const gen_step = b.step("gen", "Regenerate config files");
gen_step.dependOn(&gen_cmd.step);
const gen_version_data_cmd = b.addRunArtifact(gen_exe);
gen_version_data_cmd.addArgs(&.{ "--generate-version-data", data_version });
if (data_version_path) |path| {
gen_version_data_cmd.addArg("--langref_path");
gen_version_data_cmd.addFileArg(.{ .cwd_relative = path });
}
const version_data_file_name = if (data_version_path != null)
b.fmt("version_data_{s}.zig", .{data_version})
else blk: {
// invalidate version data periodically from cache because the website content may change
// setting `has_side_effects` would also be possible but that would always force a re-run
const timestamp = @divFloor(std.time.timestamp(), std.time.s_per_day);
break :blk b.fmt("version_data_{s}_{d}.zig", .{ data_version, timestamp });
};
gen_version_data_cmd.addArg("--generate-version-data-path");
const version_data_path: std.build.LazyPath = if (override_version_data_file_path) |path|
.{ .cwd_relative = path }
else
gen_version_data_cmd.addOutputFileArg(version_data_file_name);
const version_data_module = b.addModule("version_data", .{ .source_file = version_data_path });
exe.addModule("version_data", version_data_module);
const zls_module = b.addModule("zls", .{
.source_file = .{ .path = "src/zls.zig" },
.dependencies = &.{
.{ .name = "known-folders", .module = known_folders_module },
.{ .name = "diffz", .module = diffz_module },
.{ .name = "build_options", .module = build_options_module },
.{ .name = "version_data", .module = version_data_module },
},
});
const test_step = b.step("test", "Run all the tests");
test_step.dependOn(b.getInstallStep());
var tests = b.addTest(.{
.root_source_file = .{ .path = "tests/tests.zig" },
.target = target,
.optimize = optimize,
.filter = test_filter,
.single_threaded = single_threaded,
});
tests.addModule("zls", zls_module);
tests.addModule("build_options", build_options_module);
tests.addModule("test_options", test_options_module);
test_step.dependOn(&b.addRunArtifact(tests).step);
var src_tests = b.addTest(.{
.root_source_file = .{ .path = "src/zls.zig" },
.target = target,
.optimize = optimize,
.filter = test_filter,
.single_threaded = single_threaded,
});
src_tests.addModule("build_options", build_options_module);
src_tests.addModule("test_options", test_options_module);
test_step.dependOn(&b.addRunArtifact(src_tests).step);
if (coverage) {
const include_pattern = b.fmt("--include-pattern=/src", .{});
const exclude_pattern = b.fmt("--exclude-pattern=/src/stage2", .{});
const args = &[_]std.build.RunStep.Arg{
.{ .bytes = b.dupe("kcov") },
.{ .bytes = b.dupe("--collect-only") },
.{ .bytes = b.dupe(include_pattern) },
.{ .bytes = b.dupe(exclude_pattern) },
.{ .bytes = b.dupe(coverage_output_dir) },
};
var tests_run = b.addRunArtifact(tests);
var src_tests_run = b.addRunArtifact(src_tests);
tests_run.has_side_effects = true;
src_tests_run.has_side_effects = true;
tests_run.argv.insertSlice(0, args) catch @panic("OOM");
src_tests_run.argv.insertSlice(0, args) catch @panic("OOM");
var merge_step = std.build.RunStep.create(b, "merge kcov");
merge_step.has_side_effects = true;
merge_step.addArgs(&.{
"kcov",
"--merge",
coverage_output_dir,
b.pathJoin(&.{ coverage_output_dir, "test" }),
});
merge_step.step.dependOn(&b.addRemoveDirTree(coverage_output_dir).step);
merge_step.step.dependOn(&tests_run.step);
merge_step.step.dependOn(&src_tests_run.step);
test_step.dependOn(&merge_step.step);
}
}