| ... | ... | @@ -0,0 +1,83 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | pub fn build(b: *std.Build) void { |
| 4 | const test_step = b.step("test", "Test"); |
| 5 | b.default_step = test_step; |
| 6 | |
| 7 | const libfoo = b.addStaticLibrary(.{ |
| 8 | .name = "foo", |
| 9 | .target = b.resolveTargetQuery(.{}), |
| 10 | .optimize = .Debug, |
| 11 | }); |
| 12 | libfoo.addCSourceFile(.{ .file = b.addWriteFiles().add("empty.c", "") }); |
| 13 | |
| 14 | const exe = b.addExecutable(.{ |
| 15 | .name = "exe", |
| 16 | .target = b.resolveTargetQuery(.{}), |
| 17 | .optimize = .Debug, |
| 18 | .link_libc = true, |
| 19 | }); |
| 20 | exe.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c", |
| 21 | \\#include <stdio.h> |
| 22 | \\#include <foo/a.h> |
| 23 | \\#include <foo/sub_dir/b.h> |
| 24 | \\#include <foo/d.h> |
| 25 | \\#include <foo/config.h> |
| 26 | \\int main(void) { |
| 27 | \\ printf(FOO_A FOO_B FOO_D FOO_CONFIG_1 FOO_CONFIG_2); |
| 28 | \\ return 0; |
| 29 | \\} |
| 30 | ) }); |
| 31 | |
| 32 | libfoo.installHeaders(.{ .path = "include" }, "foo", .{ .exclude_extensions = &.{".ignore_me.h"} }); |
| 33 | libfoo.installHeader(b.addWriteFiles().add("d.h", |
| 34 | \\#define FOO_D "D" |
| 35 | \\ |
| 36 | ), "foo/d.h"); |
| 37 | |
| 38 | if (libfoo.installed_headers_include_tree != null) std.debug.panic("include tree step was created before linking", .{}); |
| 39 | |
| 40 | // Link before we have registered all headers for installation, |
| 41 | // to verify that the lazily created write files step is properly taken into account. |
| 42 | exe.linkLibrary(libfoo); |
| 43 | |
| 44 | if (libfoo.installed_headers_include_tree == null) std.debug.panic("include tree step was not created after linking", .{}); |
| 45 | |
| 46 | libfoo.installConfigHeader(b.addConfigHeader(.{ |
| 47 | .style = .blank, |
| 48 | .include_path = "foo/config.h", |
| 49 | }, .{ |
| 50 | .FOO_CONFIG_1 = "1", |
| 51 | .FOO_CONFIG_2 = "2", |
| 52 | })); |
| 53 | |
| 54 | const run_exe = b.addRunArtifact(exe); |
| 55 | run_exe.expectStdOutEqual("ABD12"); |
| 56 | test_step.dependOn(&run_exe.step); |
| 57 | |
| 58 | const install_exe = b.addInstallArtifact(libfoo, .{ |
| 59 | .dest_dir = .{ .override = .{ .custom = "custom" } }, |
| 60 | .h_dir = .{ .override = .{ .custom = "custom/include" } }, |
| 61 | .implib_dir = .disabled, |
| 62 | .pdb_dir = .disabled, |
| 63 | }); |
| 64 | const check_exists = b.addExecutable(.{ |
| 65 | .name = "check_exists", |
| 66 | .root_source_file = .{ .path = "check_exists.zig" }, |
| 67 | .target = b.resolveTargetQuery(.{}), |
| 68 | .optimize = .Debug, |
| 69 | }); |
| 70 | const run_check_exists = b.addRunArtifact(check_exists); |
| 71 | run_check_exists.addArgs(&.{ |
| 72 | "custom/include/foo/a.h", |
| 73 | "!custom/include/foo/ignore_me.txt", |
| 74 | "custom/include/foo/sub_dir/b.h", |
| 75 | "!custom/include/foo/sub_dir/c.ignore_me.h", |
| 76 | "custom/include/foo/d.h", |
| 77 | "custom/include/foo/config.h", |
| 78 | }); |
| 79 | run_check_exists.setCwd(.{ .cwd_relative = b.getInstallPath(.prefix, "") }); |
| 80 | run_check_exists.expectExitCode(0); |
| 81 | run_check_exists.step.dependOn(&install_exe.step); |
| 82 | test_step.dependOn(&run_check_exists.step); |
| 83 | } |