|
| 1 | +const std = @import("std"); |
| 2 | +const FileSource = std.Build.FileSource; |
| 3 | + |
| 4 | +pub fn build(b: *std.Build) void { |
| 5 | + const target = b.standardTargetOptions(.{}); |
| 6 | + const optimize = b.standardOptimizeOption(.{}); |
| 7 | + const tests = b.option(bool, "Tests", "Build tests [default: false]") orelse false; |
| 8 | + |
| 9 | + const config = b.addConfigHeader(.{ |
| 10 | + .style = .{ |
| 11 | + .cmake = .{ .path = "sigc++config.h.cmake" }, |
| 12 | + }, |
| 13 | + .include_path = "sigc++config.h", |
| 14 | + }, .{ |
| 15 | + .SIGCXX_DISABLE_DEPRECATED = null, |
| 16 | + .SIGC_DLL = if (target.isWindows()) {} else null, |
| 17 | + ._ALLOW_KEYWORD_MACROS = if (target.getAbi() == .msvc) "1" else null, |
| 18 | + }); |
| 19 | + const lib = b.addStaticLibrary(.{ |
| 20 | + .name = "sigc++", |
| 21 | + .target = target, |
| 22 | + .optimize = optimize, |
| 23 | + }); |
| 24 | + lib.addConfigHeader(config); |
| 25 | + lib.addIncludePath("."); |
| 26 | + lib.addCSourceFiles(&.{ |
| 27 | + "sigc++/connection.cc", |
| 28 | + "sigc++/functors/slot_base.cc", |
| 29 | + "sigc++/signal_base.cc", |
| 30 | + "sigc++/trackable.cc", |
| 31 | + }, cxxFlags); |
| 32 | + lib.linkLibCpp(); |
| 33 | + lib.installHeadersDirectoryOptions(.{ |
| 34 | + .source_dir = FileSource.relative("sigc++"), |
| 35 | + .install_dir = .header, |
| 36 | + .install_subdir = "sigc++", |
| 37 | + .exclude_extensions = &.{ |
| 38 | + "am", |
| 39 | + "gitignore", |
| 40 | + "build", |
| 41 | + "cc", |
| 42 | + "txt", |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + b.installArtifact(lib); |
| 47 | + |
| 48 | + if (tests) { |
| 49 | + buildTest(b, .{ |
| 50 | + .path = "examples/hello_world.cc", |
| 51 | + .lib = lib, |
| 52 | + }); |
| 53 | + buildTest(b, .{ |
| 54 | + .path = "examples/member_method.cc", |
| 55 | + .lib = lib, |
| 56 | + }); |
| 57 | + buildTest(b, .{ |
| 58 | + .path = "tests/test_accum_iter.cc", |
| 59 | + .lib = lib, |
| 60 | + }); |
| 61 | + buildTest(b, .{ |
| 62 | + .path = "tests/test_bind.cc", |
| 63 | + .lib = lib, |
| 64 | + }); |
| 65 | + buildTest(b, .{ |
| 66 | + .path = "tests/test_compose.cc", |
| 67 | + .lib = lib, |
| 68 | + }); |
| 69 | + buildTest(b, .{ |
| 70 | + .path = "tests/test_accumulated.cc", |
| 71 | + .lib = lib, |
| 72 | + }); |
| 73 | + buildTest(b, .{ |
| 74 | + .path = "tests/test_hide.cc", |
| 75 | + .lib = lib, |
| 76 | + }); |
| 77 | + buildTest(b, .{ |
| 78 | + .path = "tests/test_slot.cc", |
| 79 | + .lib = lib, |
| 80 | + }); |
| 81 | + buildTest(b, .{ |
| 82 | + .path = "tests/test_tuple_start.cc", |
| 83 | + .lib = lib, |
| 84 | + }); |
| 85 | + buildTest(b, .{ |
| 86 | + .path = "tests/test_tuple_transform_each.cc", |
| 87 | + .lib = lib, |
| 88 | + }); |
| 89 | + buildTest(b, .{ |
| 90 | + .path = "tests/test_visit_each.cc", |
| 91 | + .lib = lib, |
| 92 | + }); |
| 93 | + buildTest(b, .{ |
| 94 | + .path = "tests/test_weak_raw_ptr.cc", |
| 95 | + .lib = lib, |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | +const cxxFlags: []const []const u8 = &.{ |
| 100 | + "-Wall", |
| 101 | + "-Wextra", |
| 102 | + "-Werror", |
| 103 | +}; |
| 104 | +fn buildTest(b: *std.Build, info: BuildInfo) void { |
| 105 | + const test_exe = b.addExecutable(.{ |
| 106 | + .name = info.filename(), |
| 107 | + .optimize = info.lib.optimize, |
| 108 | + .target = info.lib.target, |
| 109 | + }); |
| 110 | + for (info.lib.include_dirs.items) |include| { |
| 111 | + test_exe.include_dirs.append(include) catch {}; |
| 112 | + } |
| 113 | + test_exe.addCSourceFile(info.path, cxxFlags); |
| 114 | + test_exe.addCSourceFile("tests/testutilities.cc", cxxFlags); |
| 115 | + test_exe.linkLibrary(info.lib); |
| 116 | + test_exe.linkLibCpp(); |
| 117 | + b.installArtifact(test_exe); |
| 118 | + |
| 119 | + const run_cmd = b.addRunArtifact(test_exe); |
| 120 | + run_cmd.step.dependOn(b.getInstallStep()); |
| 121 | + if (b.args) |args| { |
| 122 | + run_cmd.addArgs(args); |
| 123 | + } |
| 124 | + |
| 125 | + const run_step = b.step( |
| 126 | + b.fmt("{s}", .{info.filename()}), |
| 127 | + b.fmt("Run the {s} test", .{info.filename()}), |
| 128 | + ); |
| 129 | + run_step.dependOn(&run_cmd.step); |
| 130 | +} |
| 131 | + |
| 132 | +const BuildInfo = struct { |
| 133 | + lib: *std.Build.CompileStep, |
| 134 | + path: []const u8, |
| 135 | + |
| 136 | + fn filename(self: BuildInfo) []const u8 { |
| 137 | + var split = std.mem.split(u8, std.fs.path.basename(self.path), "."); |
| 138 | + return split.first(); |
| 139 | + } |
| 140 | +}; |
0 commit comments