authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-03-03 19:55:41+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-04-07 15:34:47+02:00
log27c8f895ebeed9f124d32f6a96eaea92e06825bb
treedd52ed34844f5911002dbd68bf32d6822a295b39
parente16db2988777e18c56925b4a458914b20a183c12

Add standalone tests for `Compile.installHeaders`

Co-authored-by: Abhinav Gupta <mail@abhinavg.net>

7 files changed, 128 insertions(+), 0 deletions(-)

test/standalone.zig+4
......@@ -266,6 +266,10 @@ pub const build_cases = [_]BuildCase{
266266 .build_root = "test/standalone/depend_on_main_mod",
267267 .import = @import("standalone/depend_on_main_mod/build.zig"),
268268 },
269 .{
270 .build_root = "test/standalone/install_headers",
271 .import = @import("standalone/install_headers/build.zig"),
272 },
269273};
270274
271275const std = @import("std");
test/standalone/install_headers/build.zig created+83
......@@ -0,0 +1,83 @@
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 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}
test/standalone/install_headers/check_exists.zig created+37
......@@ -0,0 +1,37 @@
1const std = @import("std");
2
3/// Checks the existence of files relative to cwd.
4/// A path starting with ! should not exist.
5pub fn main() !void {
6 var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
7 defer arena_state.deinit();
8
9 const arena = arena_state.allocator();
10
11 var arg_it = try std.process.argsWithAllocator(arena);
12 _ = arg_it.next();
13
14 const cwd = std.fs.cwd();
15 const cwd_realpath = try cwd.realpathAlloc(arena, "");
16
17 while (arg_it.next()) |file_path| {
18 if (file_path.len > 0 and file_path[0] == '!') {
19 errdefer std.log.err(
20 "excluded file check '{s}{c}{s}' failed",
21 .{ cwd_realpath, std.fs.path.sep, file_path[1..] },
22 );
23 if (std.fs.cwd().statFile(file_path[1..])) |_| {
24 return error.FileFound;
25 } else |err| switch (err) {
26 error.FileNotFound => {},
27 else => return err,
28 }
29 } else {
30 errdefer std.log.err(
31 "included file check '{s}{c}{s}' failed",
32 .{ cwd_realpath, std.fs.path.sep, file_path },
33 );
34 _ = try std.fs.cwd().statFile(file_path);
35 }
36 }
37}
test/standalone/install_headers/include/a.h created+1
......@@ -0,0 +1 @@
1#define FOO_A "A"
test/standalone/install_headers/include/ignore_me.txt created+1
......@@ -0,0 +1 @@
1ignore me
test/standalone/install_headers/include/sub_dir/b.h created+1
......@@ -0,0 +1 @@
1#define FOO_B "B"
test/standalone/install_headers/include/sub_dir/ignore_me.h created+1
......@@ -0,0 +1 @@
1#error "ignore me"