master
  1const std = @import("std");
  2
  3pub fn build(b: *std.Build) void {
  4    const test_step = b.step("test", "Test");
  5    b.default_step = test_step;
  6
  7    const empty_c = b.addWriteFiles().add("empty.c", "");
  8
  9    const libfoo = b.addLibrary(.{
 10        .linkage = .static,
 11        .name = "foo",
 12        .root_module = b.createModule(.{
 13            .root_source_file = null,
 14            .target = b.resolveTargetQuery(.{}),
 15            .optimize = .Debug,
 16        }),
 17    });
 18    libfoo.root_module.addCSourceFile(.{ .file = empty_c });
 19
 20    const exe = b.addExecutable(.{
 21        .name = "exe",
 22        .root_module = b.createModule(.{
 23            .root_source_file = null,
 24            .target = b.resolveTargetQuery(.{}),
 25            .optimize = .Debug,
 26            .link_libc = true,
 27        }),
 28    });
 29    exe.root_module.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c",
 30        \\#include <stdio.h>
 31        \\#include <foo/a.h>
 32        \\#include <foo/sub_dir/b.h>
 33        \\#include <foo/d.h>
 34        \\#include <foo/config.h>
 35        \\#include <bar.h>
 36        \\int main(void) {
 37        \\    printf(FOO_A FOO_B FOO_D FOO_CONFIG_1 FOO_CONFIG_2 BAR_X);
 38        \\    return 0;
 39        \\}
 40    ) });
 41
 42    libfoo.installHeadersDirectory(b.path("include"), "foo", .{ .exclude_extensions = &.{".ignore_me.h"} });
 43    libfoo.installHeader(b.addWriteFiles().add("d.h",
 44        \\#define FOO_D "D"
 45        \\
 46    ), "foo/d.h");
 47
 48    if (libfoo.installed_headers_include_tree != null) std.debug.panic("include tree step was created before linking", .{});
 49
 50    // Link (and get the include tree) before we have registered all headers for installation,
 51    // to verify that the lazily created write files step is properly taken into account.
 52    exe.root_module.linkLibrary(libfoo);
 53    _ = libfoo.getEmittedIncludeTree();
 54
 55    if (libfoo.installed_headers_include_tree == null) std.debug.panic("include tree step was not created after linking", .{});
 56
 57    libfoo.installConfigHeader(b.addConfigHeader(.{
 58        .style = .blank,
 59        .include_path = "foo/config.h",
 60    }, .{
 61        .FOO_CONFIG_1 = "1",
 62        .FOO_CONFIG_2 = "2",
 63    }));
 64
 65    const libbar = b.addLibrary(.{
 66        .linkage = .static,
 67        .name = "bar",
 68        .root_module = b.createModule(.{
 69            .root_source_file = null,
 70            .target = b.resolveTargetQuery(.{}),
 71            .optimize = .Debug,
 72        }),
 73    });
 74    libbar.root_module.addCSourceFile(.{ .file = empty_c });
 75    libbar.installHeader(b.addWriteFiles().add("bar.h",
 76        \\#define BAR_X "X"
 77        \\
 78    ), "bar.h");
 79    libfoo.installLibraryHeaders(libbar);
 80
 81    const run_exe = b.addRunArtifact(exe);
 82    run_exe.expectStdOutEqual("ABD12X");
 83    test_step.dependOn(&run_exe.step);
 84
 85    const install_libfoo = b.addInstallArtifact(libfoo, .{
 86        .dest_dir = .{ .override = .{ .custom = "custom" } },
 87        .h_dir = .{ .override = .{ .custom = "custom/include" } },
 88        .implib_dir = .disabled,
 89        .pdb_dir = .disabled,
 90    });
 91    const check_exists = b.addExecutable(.{
 92        .name = "check_exists",
 93        .root_module = b.createModule(.{
 94            .root_source_file = b.path("check_exists.zig"),
 95            .target = b.resolveTargetQuery(.{}),
 96            .optimize = .Debug,
 97        }),
 98    });
 99    const run_check_exists = b.addRunArtifact(check_exists);
100    run_check_exists.addArgs(&.{
101        "custom/include/foo/a.h",
102        "!custom/include/foo/ignore_me.txt",
103        "custom/include/foo/sub_dir/b.h",
104        "!custom/include/foo/sub_dir/c.ignore_me.h",
105        "custom/include/foo/d.h",
106        "custom/include/foo/config.h",
107        "custom/include/bar.h",
108    });
109    run_check_exists.setCwd(.{ .cwd_relative = b.getInstallPath(.prefix, "") });
110    run_check_exists.expectExitCode(0);
111    run_check_exists.step.dependOn(&install_libfoo.step);
112    test_step.dependOn(&run_check_exists.step);
113}