-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.zig
More file actions
executable file
·110 lines (88 loc) · 3.86 KB
/
build.zig
File metadata and controls
executable file
·110 lines (88 loc) · 3.86 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
const STD = @import("std");
const COMPILE_FLAGZ = @import("compile_flagz");
const CONFIG = @import("config");
const BUILTIN = @import("builtin");
pub fn build(b: *STD.Build) !void {
if (BUILTIN.zig_version.minor < 15) {
@compileError("Zig >= v0.15.1 is required...");
}
const TARGET = b.standardTargetOptions(.{});
const OPTIMIZE = b.standardOptimizeOption(.{});
// dependencies
const DEP_GTEST = b.dependency("googletest", .{});
// cli
{
const CPPLINGS_CLI = b.addExecutable(.{ .name = "cpplings_cli", .root_module = b.createModule(.{
.target = TARGET,
.optimize = OPTIMIZE,
.root_source_file = b.path("src/main.zig"),
}) });
b.installArtifact(CPPLINGS_CLI);
const CPPLINGS_CLI_ARTIFIACT = b.addRunArtifact(CPPLINGS_CLI);
const CPPLINGS_CLI_RUN_STEP = b.step("run", "Run cpplings cli");
CPPLINGS_CLI_RUN_STEP.dependOn(&CPPLINGS_CLI_ARTIFIACT.step);
CPPLINGS_CLI_ARTIFIACT.step.dependOn(b.getInstallStep());
if (b.args) |args| {
if (args.len > 0) {
CPPLINGS_CLI_ARTIFIACT.addArgs(args);
}
}
}
// exercises
{
const COMPILER_FLAGS = [_][]const u8{ "-std=c++23", "-Wall", "-Werror", "-Wextra" };
const CPPLINGS_EXERCISE = b.addExecutable(.{
.name = "cpplings_exercise",
.root_module = b.createModule(.{
.target = TARGET,
.optimize = OPTIMIZE,
.link_libc = true,
.link_libcpp = true,
}),
});
if (b.args) |args| {
if (args.len > 0) {
const EXERCISE_FILENAMES = args;
CPPLINGS_EXERCISE.root_module.addCSourceFiles(.{ .flags = &COMPILER_FLAGS, .files = EXERCISE_FILENAMES });
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("include"));
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("exercises"));
CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest"));
// CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest_main"));
b.installArtifact(CPPLINGS_EXERCISE);
}
}
const CPPLINGS_EXERCISE_ARTIFACT = b.addRunArtifact(CPPLINGS_EXERCISE);
const CPPLINGS_RUN_EXERCISE_STEP = b.step("exercises", "Build and run cppligns_exercise exercise");
CPPLINGS_RUN_EXERCISE_STEP.dependOn(&CPPLINGS_EXERCISE_ARTIFACT.step);
CPPLINGS_EXERCISE_ARTIFACT.step.dependOn(b.getInstallStep());
if (b.args) |args| {
if (args.len > 0) {
CPPLINGS_EXERCISE_ARTIFACT.addArgs(args);
}
}
}
// create compile flags generator
{
var cflags = COMPILE_FLAGZ.addCompileFlags(b);
cflags.addIncludePath(b.path("include"));
cflags.addIncludePath(b.path("src"));
cflags.addIncludePath(DEP_GTEST.path("include"));
const CLANG_PLUS_PLUS = try STD.process.Child.run(.{ .allocator = b.allocator, .argv = &[_][]const u8{ "zig", "c++", "-E", "-x", "c++", "-", "-v" } });
var clang_plus_plus_output = STD.mem.splitScalar(u8, CLANG_PLUS_PLUS.stderr, '\n');
var start_capture = false;
while (clang_plus_plus_output.next()) |line| {
if (STD.mem.startsWith(u8, line, "#include <...> search starts here:")) {
start_capture = true;
continue;
}
if (STD.mem.startsWith(u8, line, "End of search list.")) {
break;
}
if (start_capture) {
cflags.addIncludePath(.{ .cwd_relative = STD.mem.trim(u8, line, " ") });
}
}
const CFLAGS_STEP = b.step("compile-flags", "Generate compile_flags.txt for C/C++ IDE support");
CFLAGS_STEP.dependOn(&cflags.step);
}
}