|
1 | 1 | const std = @import("std"); |
2 | | -const zcc = @import("compile_commands"); |
| 2 | +const compile_flagz = @import("compile_flagz"); |
| 3 | +const config = @import("config"); |
| 4 | +const builtin = @import("builtin"); |
3 | 5 |
|
4 | 6 | pub fn build(b: *std.Build) !void { |
5 | | - // const splash_screen = |
6 | | - // \\ _ _ |
7 | | - // \\ | (_) |
8 | | - // \\ ___ _ __ _ __ | |_ _ __ __ _ ___ |
9 | | - // \\ / __| '_ \| '_ \| | | '_ \ / _` / __| |
10 | | - // \\ | (__| |_) | |_) | | | | | | (_| \__ \ |
11 | | - // \\ \___| .__/| .__/|_|_|_| |_|\__, |___/ |
12 | | - // \\ | | | | __/ | |
13 | | - // \\ |_| |_| |___/ |
14 | | - // \\ |
15 | | - // \\ compiling... |
16 | | - // ; |
17 | | - |
18 | | - // std.debug.print("{s}", .{splash_screen}); |
| 7 | + if (builtin.zig_version.minor < 15) { |
| 8 | + @compileError("Zig >= v0.15.1 is required..."); |
| 9 | + } |
19 | 10 |
|
20 | 11 | const target = b.standardTargetOptions(.{}); |
21 | 12 | const optimize = b.standardOptimizeOption(.{}); |
22 | 13 | const compiler_flags = [_][]const u8{ "-std=c++23", "-Wall", "-Werror", "-Wextra" }; |
23 | | - var targets = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); |
24 | | - |
25 | | - const allocator = std.heap.page_allocator; |
26 | | - |
27 | | - const exe = b.addExecutable(.{ .name = "cpplings", .target = target, .optimize = optimize, .link_libc = true }); |
28 | | - |
29 | | - // main.cpp |
30 | | - exe.addCSourceFile(.{ .file = b.path("src/main.cpp"), .flags = &compiler_flags }); |
31 | | - |
32 | | - // link C/C++ std libraries |
33 | | - exe.linkLibC(); |
34 | | - exe.linkLibCpp(); |
35 | | - |
36 | | - // add include path |
37 | | - exe.addIncludePath(b.path("include")); |
38 | | - |
39 | | - // add source files |
40 | | - // chapters |
41 | | - const chapter_dirs = try findSourceFiles(allocator, "exercises/", "", false); |
42 | | - |
43 | | - // find exercises in each chapter folder |
44 | | - for (chapter_dirs) |chapter_dir| { |
45 | | - // std.debug.print("\n{s}\n-------------\n", .{chapter_dir}); |
46 | 14 |
|
47 | | - const exercises = try findSourceFiles(allocator, chapter_dir, "cpp", true); |
| 15 | + // dependencies |
| 16 | + const dep_gtest = b.dependency("googletest", .{}); |
48 | 17 |
|
49 | | - // add exercise source file |
50 | | - for (exercises) |exercise| { |
51 | | - // std.debug.print("{s}\n", .{exercise}); |
52 | | - exe.addCSourceFile(.{ .file = b.path(exercise), .flags = &compiler_flags }); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - // dependency injection |
57 | | - // configure and add gtest |
58 | | - const googletest_dep = b.dependency("googletest", .{ |
| 18 | + // cli |
| 19 | + const cpplings_cli = b.addExecutable(.{ .name = "cpplings_cli", .root_module = b.createModule(.{ |
59 | 20 | .target = target, |
60 | 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("cli", "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 = b.addExecutable(.{ |
| 34 | + .name = "cpplings", |
| 35 | + .root_module = b.createModule(.{ |
| 36 | + .target = target, |
| 37 | + .optimize = optimize, |
| 38 | + .link_libc = true, |
| 39 | + .link_libcpp = true, |
| 40 | + }), |
61 | 41 | }); |
62 | 42 |
|
63 | | - exe.linkLibrary(googletest_dep.artifact("gtest")); |
| 43 | + if (b.args) |args| { |
| 44 | + if (args.len > 0) { |
| 45 | + const exercise_filenames = args; |
64 | 46 |
|
65 | | - // linking |
66 | | - b.installArtifact(exe); |
| 47 | + cpplings.root_module.addCSourceFiles(.{ .flags = &compiler_flags, .files = exercise_filenames }); |
67 | 48 |
|
68 | | - targets.append(exe) catch @panic("OOM"); |
69 | | - zcc.createStep(b, "compile-commands", targets.toOwnedSlice() catch @panic("OOM")); |
| 49 | + cpplings.root_module.addIncludePath(b.path("include")); |
| 50 | + cpplings.root_module.linkLibrary(dep_gtest.artifact("gtest")); |
| 51 | + cpplings.root_module.linkLibrary(dep_gtest.artifact("gtest_main")); |
70 | 52 |
|
71 | | - // run command |
72 | | - const run_exe = b.addRunArtifact(exe); |
73 | | - const run_step = b.step("run", "run cpplings"); |
74 | | - run_step.dependOn(&run_exe.step); |
75 | | -} |
| 53 | + b.installArtifact(cpplings); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const cpplings_run_exercise_step = b.step("exercises", "Build and run cpplings exercise"); |
| 58 | + const cpplings_run_exercise_cmd = b.addRunArtifact(cpplings); |
| 59 | + cpplings_run_exercise_step.dependOn(&cpplings_run_exercise_cmd.step); |
| 60 | + cpplings_run_exercise_cmd.step.dependOn(b.getInstallStep()); |
76 | 61 |
|
77 | | -/// find source files in directory |
78 | | -pub fn findSourceFiles( |
79 | | - /// allocator |
80 | | - allocator: std.mem.Allocator, |
81 | | - /// string literal of directory containing source files |
82 | | - directory: anytype, |
83 | | - /// extension of file |
84 | | - extension: anytype, |
85 | | - /// toggle filtering by extension |
86 | | - is_toggle_filtering_files: bool, |
87 | | -) ![][]const u8 { |
88 | | - // _ = is_toggle_filtering_files; |
89 | | - |
90 | | - var files = std.ArrayList([]const u8).init(allocator); |
91 | | - |
92 | | - const dir = try std.fs.cwd().openDir(directory, .{ .iterate = true }); |
93 | | - |
94 | | - var iter = dir.iterate(); |
95 | | - |
96 | | - while (try iter.next()) |entry| { |
97 | | - if (!is_toggle_filtering_files) { |
98 | | - try files.append(try std.mem.concat(allocator, u8, &[_][]const u8{ directory, entry.name, "/" })); |
99 | | - } else { |
100 | | - if (std.mem.containsAtLeast(u8, entry.name, 1, extension)) { |
101 | | - // try files.append(entry.name); |
102 | | - try files.append(try std.mem.concat(allocator, u8, &[_][]const u8{ directory, entry.name })); |
103 | | - } |
| 62 | + if (b.args) |args| { |
| 63 | + if (args.len > 0) { |
| 64 | + cpplings_run_exercise_cmd.addArgs(args); |
104 | 65 | } |
105 | 66 | } |
106 | 67 |
|
107 | | - return files.items; |
| 68 | + // create compile flags generator |
| 69 | + var cflags = compile_flagz.addCompileFlags(b); |
| 70 | + cflags.addIncludePath(b.path("include")); |
| 71 | + cflags.addIncludePath(b.path("src")); |
| 72 | + cflags.addIncludePath(dep_gtest.path("include")); |
| 73 | + |
| 74 | + // TODO: automate libcc path |
| 75 | + // $ clang++ -E -x c++ - -v < /dev/null |
| 76 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/a3c2pnnyycikxs9gnxgakvilajyxhyv2-lldb-19.1.7-dev/include" }); |
| 77 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include" }); |
| 78 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/n7p5cdg3d55fr659qm8h0vynl3rcf26h-compiler-rt-libc-19.1.7-dev/include" }); |
| 79 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include/c++/14.3.0" }); |
| 80 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include/c++/14.3.0/x86_64-unknown-linux-gnu" }); |
| 81 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/fbfcll570w9vimfbh41f9b4rrwnp33f3-clang-wrapper-19.1.7/resource-root/include" }); |
| 82 | + cflags.addIncludePath(.{ .cwd_relative = "/nix/store/gf3wh0x0rzb1dkx0wx1jvmipydwfzzd5-glibc-2.40-66-dev/include" }); |
| 83 | + |
| 84 | + const cflags_step = b.step("compile-flags", "Generate compile_flags.txt for C/C++ IDE support"); |
| 85 | + cflags_step.dependOn(&cflags.step); |
108 | 86 | } |
0 commit comments