Skip to content

Commit 023e4c3

Browse files
feat(build): clean up build.zig
- seperate build steps by nested scopes - comments for different build steps
1 parent 2a34410 commit 023e4c3

1 file changed

Lines changed: 77 additions & 62 deletions

File tree

build.zig

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,87 +10,102 @@ pub fn build(b: *STD.Build) !void {
1010

1111
const TARGET = b.standardTargetOptions(.{});
1212
const OPTIMIZE = b.standardOptimizeOption(.{});
13-
const COMPILER_FLAGS = [_][]const u8{ "-std=c++23", "-Wall", "-Werror", "-Wextra" };
1413

1514
// dependencies
1615
const DEP_GTEST = b.dependency("googletest", .{});
1716

1817
// cli
19-
const CPPLINGS_CLI = b.addExecutable(.{ .name = "cpplings_cli", .root_module = b.createModule(.{
20-
.target = TARGET,
21-
.optimize = OPTIMIZE,
22-
.root_source_file = b.path("src/main.zig"),
23-
}) });
24-
25-
b.installArtifact(CPPLINGS_CLI);
26-
27-
const CPPLINGS_CLI_STEP = b.step("run", "Run cpplings cli");
28-
const CPPLINGS_CLI_CMD = b.addRunArtifact(CPPLINGS_CLI);
29-
CPPLINGS_CLI_STEP.dependOn(&CPPLINGS_CLI_CMD.step);
30-
CPPLINGS_CLI_CMD.step.dependOn(b.getInstallStep());
31-
32-
// exercises
33-
const CPPLINGS_EXERCISE = b.addExecutable(.{
34-
.name = "cpplings_exercise",
35-
.root_module = b.createModule(.{
18+
{
19+
const CPPLINGS_CLI = b.addExecutable(.{ .name = "cpplings_cli", .root_module = b.createModule(.{
3620
.target = TARGET,
3721
.optimize = OPTIMIZE,
38-
.link_libc = true,
39-
.link_libcpp = true,
40-
}),
41-
});
22+
.root_source_file = b.path("src/main.zig"),
23+
}) });
4224

43-
if (b.args) |args| {
44-
if (args.len > 0) {
45-
const EXERCISE_FILENAMES = args;
25+
b.installArtifact(CPPLINGS_CLI);
26+
const CPPLINGS_CLI_ARTIFIACT = b.addRunArtifact(CPPLINGS_CLI);
4627

47-
CPPLINGS_EXERCISE.root_module.addCSourceFiles(.{ .flags = &COMPILER_FLAGS, .files = EXERCISE_FILENAMES });
28+
const CPPLINGS_CLI_RUN_STEP = b.step("run", "Run cpplings cli");
29+
CPPLINGS_CLI_RUN_STEP.dependOn(&CPPLINGS_CLI_ARTIFIACT.step);
30+
CPPLINGS_CLI_ARTIFIACT.step.dependOn(b.getInstallStep());
4831

49-
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("include"));
50-
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("exercises"));
51-
CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest"));
52-
// CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest_main"));
53-
54-
b.installArtifact(CPPLINGS_EXERCISE);
32+
if (b.args) |args| {
33+
if (args.len > 0) {
34+
CPPLINGS_CLI_ARTIFIACT.addArgs(args);
35+
}
5536
}
5637
}
5738

58-
const CPPLINGS_RUN_EXERCISE_STEP = b.step("exercises", "Build and run cppligns_exercise exercise");
59-
const CPPLINGS_RUN_EXERCISE_CMD = b.addRunArtifact(CPPLINGS_EXERCISE);
60-
CPPLINGS_RUN_EXERCISE_STEP.dependOn(&CPPLINGS_RUN_EXERCISE_CMD.step);
61-
CPPLINGS_RUN_EXERCISE_CMD.step.dependOn(b.getInstallStep());
39+
// exercises
40+
{
41+
const COMPILER_FLAGS = [_][]const u8{ "-std=c++23", "-Wall", "-Werror", "-Wextra" };
42+
43+
const CPPLINGS_EXERCISE = b.addExecutable(.{
44+
.name = "cpplings_exercise",
45+
.root_module = b.createModule(.{
46+
.target = TARGET,
47+
.optimize = OPTIMIZE,
48+
.link_libc = true,
49+
.link_libcpp = true,
50+
}),
51+
});
52+
53+
if (b.args) |args| {
54+
if (args.len > 0) {
55+
const EXERCISE_FILENAMES = args;
56+
57+
CPPLINGS_EXERCISE.root_module.addCSourceFiles(.{ .flags = &COMPILER_FLAGS, .files = EXERCISE_FILENAMES });
58+
59+
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("include"));
60+
CPPLINGS_EXERCISE.root_module.addIncludePath(b.path("exercises"));
61+
CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest"));
62+
// CPPLINGS_EXERCISE.root_module.linkLibrary(DEP_GTEST.artifact("gtest_main"));
63+
64+
b.installArtifact(CPPLINGS_EXERCISE);
65+
}
66+
}
67+
68+
const CPPLINGS_EXERCISE_ARTIFACT = b.addRunArtifact(CPPLINGS_EXERCISE);
69+
70+
const CPPLINGS_RUN_EXERCISE_STEP = b.step("exercises", "Build and run cppligns_exercise exercise");
71+
CPPLINGS_RUN_EXERCISE_STEP.dependOn(&CPPLINGS_EXERCISE_ARTIFACT.step);
72+
CPPLINGS_EXERCISE_ARTIFACT.step.dependOn(b.getInstallStep());
6273

63-
if (b.args) |args| {
64-
if (args.len > 0) {
65-
CPPLINGS_RUN_EXERCISE_CMD.addArgs(args);
74+
if (b.args) |args| {
75+
if (args.len > 0) {
76+
CPPLINGS_EXERCISE_ARTIFACT.addArgs(args);
77+
}
6678
}
6779
}
6880

6981
// create compile flags generator
70-
var cflags = COMPILE_FLAGZ.addCompileFlags(b);
71-
cflags.addIncludePath(b.path("include"));
72-
cflags.addIncludePath(b.path("src"));
73-
cflags.addIncludePath(DEP_GTEST.path("include"));
74-
75-
const CLANG_PLUS_PLUS = try STD.process.Child.run(.{ .allocator = b.allocator, .argv = &[_][]const u8{ "zig", "c++", "-E", "-x", "c++", "-", "-v" } });
76-
var clang_plus_plus_output = STD.mem.splitScalar(u8, CLANG_PLUS_PLUS.stderr, '\n');
77-
var start_capture = false;
78-
79-
while (clang_plus_plus_output.next()) |line| {
80-
if (STD.mem.startsWith(u8, line, "#include <...> search starts here:")) {
81-
start_capture = true;
82-
continue;
83-
}
84-
85-
if (STD.mem.startsWith(u8, line, "End of search list.")) {
86-
break;
82+
{
83+
var cflags = COMPILE_FLAGZ.addCompileFlags(b);
84+
cflags.addIncludePath(b.path("include"));
85+
cflags.addIncludePath(b.path("src"));
86+
cflags.addIncludePath(DEP_GTEST.path("include"));
87+
88+
const CLANG_PLUS_PLUS = try STD.process.Child.run(.{ .allocator = b.allocator, .argv = &[_][]const u8{ "zig", "c++", "-E", "-x", "c++", "-", "-v" } });
89+
var clang_plus_plus_output = STD.mem.splitScalar(u8, CLANG_PLUS_PLUS.stderr, '\n');
90+
var start_capture = false;
91+
92+
while (clang_plus_plus_output.next()) |line| {
93+
if (STD.mem.startsWith(u8, line, "#include <...> search starts here:")) {
94+
start_capture = true;
95+
continue;
96+
}
97+
98+
if (STD.mem.startsWith(u8, line, "End of search list.")) {
99+
break;
100+
}
101+
102+
if (start_capture) {
103+
cflags.addIncludePath(.{ .cwd_relative = STD.mem.trim(u8, line, " ") });
104+
}
87105
}
88106

89-
if (start_capture) {
90-
cflags.addIncludePath(.{ .cwd_relative = STD.mem.trim(u8, line, " ") });
91-
}
107+
const CFLAGS_STEP = b.step("compile-flags", "Generate compile_flags.txt for C/C++ IDE support");
108+
CFLAGS_STEP.dependOn(&cflags.step);
92109
}
93-
94-
const CFLAGS_STEP = b.step("compile-flags", "Generate compile_flags.txt for C/C++ IDE support");
95-
CFLAGS_STEP.dependOn(&cflags.step);
96110
}
111+

0 commit comments

Comments
 (0)