1const std = @import("std");
  2const builtin = @import("builtin");
  3
  4const Build = std.Build;
  5const LazyPath = Build.LazyPath;
  6const Step = Build.Step;
  7const Run = Step.Run;
  8const WriteFile = Step.WriteFile;
  9
 10pub fn build(b: *Build) void {
 11    const nb_files = b.option(u32, "nb_files", "Number of c files to generate.") orelse 10;
 12
 13    const test_step = b.step("test", "Test it");
 14    b.default_step = test_step;
 15
 16    // generate c files
 17    const files = b.allocator.alloc(LazyPath, nb_files) catch unreachable;
 18    defer b.allocator.free(files);
 19    {
 20        for (files[0 .. nb_files - 1], 1..nb_files) |*file, i| {
 21            const wf = WriteFile.create(b);
 22            file.* = wf.add(b.fmt("src_{}.c", .{i}), b.fmt(
 23                \\extern int foo_0();
 24                \\extern int bar_{}();
 25                \\extern int one_{};
 26                \\int one_{} = 1;
 27                \\int foo_{}() {{ return one_{} + foo_0(); }}
 28                \\int bar_{}() {{ return bar_{}(); }}
 29            , .{ i - 1, i - 1, i, i, i - 1, i, i - 1 }));
 30        }
 31
 32        {
 33            const wf = WriteFile.create(b);
 34            files[nb_files - 1] = wf.add("src_last.c", b.fmt(
 35                \\extern int foo_0();
 36                \\extern int bar_{}();
 37                \\extern int one_{};
 38                \\int foo_last() {{ return one_{} + foo_0(); }}
 39                \\int bar_last() {{ return bar_{}(); }}
 40            , .{ nb_files - 1, nb_files - 1, nb_files - 1, nb_files - 1 }));
 41        }
 42    }
 43
 44    add(b, test_step, files, .Debug);
 45    add(b, test_step, files, .ReleaseSafe);
 46    add(b, test_step, files, .ReleaseSmall);
 47    add(b, test_step, files, .ReleaseFast);
 48}
 49
 50fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.builtin.OptimizeMode) void {
 51    const flags = [_][]const u8{
 52        "-Wall",
 53        "-std=c11",
 54    };
 55
 56    // all files at once
 57    {
 58        const exe = b.addExecutable(.{
 59            .name = "test1",
 60            .root_module = b.createModule(.{
 61                .root_source_file = b.path("main.zig"),
 62                .optimize = optimize,
 63                .target = b.graph.host,
 64            }),
 65        });
 66
 67        for (files) |file| {
 68            exe.root_module.addCSourceFile(.{ .file = file, .flags = &flags });
 69        }
 70
 71        const run_cmd = b.addRunArtifact(exe);
 72        run_cmd.skip_foreign_checks = true;
 73        run_cmd.expectExitCode(0);
 74
 75        test_step.dependOn(&run_cmd.step);
 76    }
 77
 78    // using static librairies
 79    {
 80        const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
 81        const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
 82
 83        for (files, 1..) |file, i| {
 84            const mod = if (i & 1 == 0) mod_a else mod_b;
 85            mod.addCSourceFile(.{ .file = file, .flags = &flags });
 86        }
 87
 88        const lib_a = b.addLibrary(.{
 89            .linkage = .static,
 90            .name = "test2_a",
 91            .root_module = mod_a,
 92        });
 93        const lib_b = b.addLibrary(.{
 94            .linkage = .static,
 95            .name = "test2_b",
 96            .root_module = mod_b,
 97        });
 98
 99        const exe = b.addExecutable(.{
100            .name = "test2",
101            .root_module = b.createModule(.{
102                .root_source_file = b.path("main.zig"),
103                .target = b.graph.host,
104                .optimize = optimize,
105            }),
106        });
107        exe.root_module.linkLibrary(lib_a);
108        exe.root_module.linkLibrary(lib_b);
109
110        const run_cmd = b.addRunArtifact(exe);
111        run_cmd.skip_foreign_checks = true;
112        run_cmd.expectExitCode(0);
113
114        test_step.dependOn(&run_cmd.step);
115    }
116
117    // using static librairies and object files
118    {
119        const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
120        const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
121
122        for (files, 1..) |file, i| {
123            const obj_mod = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
124            obj_mod.addCSourceFile(.{ .file = file, .flags = &flags });
125
126            const obj = b.addObject(.{
127                .name = b.fmt("obj_{}", .{i}),
128                .root_module = obj_mod,
129            });
130
131            const lib_mod = if (i & 1 == 0) mod_a else mod_b;
132            lib_mod.addObject(obj);
133        }
134
135        const lib_a = b.addLibrary(.{
136            .linkage = .static,
137            .name = "test3_a",
138            .root_module = mod_a,
139        });
140        const lib_b = b.addLibrary(.{
141            .linkage = .static,
142            .name = "test3_b",
143            .root_module = mod_b,
144        });
145
146        const exe = b.addExecutable(.{
147            .name = "test3",
148            .root_module = b.createModule(.{
149                .root_source_file = b.path("main.zig"),
150                .target = b.graph.host,
151                .optimize = optimize,
152            }),
153        });
154        exe.root_module.linkLibrary(lib_a);
155        exe.root_module.linkLibrary(lib_b);
156
157        const run_cmd = b.addRunArtifact(exe);
158        run_cmd.skip_foreign_checks = true;
159        run_cmd.expectExitCode(0);
160
161        test_step.dependOn(&run_cmd.step);
162    }
163}