Add standalone tests for Compile.installHeaders

Co-authored-by: Abhinav Gupta <mail@abhinavg.net>
This commit is contained in:
Carl Åstholm 2024-03-03 19:55:41 +01:00
parent e16db29887
commit 27c8f895eb
7 changed files with 128 additions and 0 deletions

View File

@ -266,6 +266,10 @@ pub const build_cases = [_]BuildCase{
.build_root = "test/standalone/depend_on_main_mod", .build_root = "test/standalone/depend_on_main_mod",
.import = @import("standalone/depend_on_main_mod/build.zig"), .import = @import("standalone/depend_on_main_mod/build.zig"),
}, },
.{
.build_root = "test/standalone/install_headers",
.import = @import("standalone/install_headers/build.zig"),
},
}; };
const std = @import("std"); const std = @import("std");

View File

@ -0,0 +1,83 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Test");
b.default_step = test_step;
const libfoo = b.addStaticLibrary(.{
.name = "foo",
.target = b.resolveTargetQuery(.{}),
.optimize = .Debug,
});
libfoo.addCSourceFile(.{ .file = b.addWriteFiles().add("empty.c", "") });
const exe = b.addExecutable(.{
.name = "exe",
.target = b.resolveTargetQuery(.{}),
.optimize = .Debug,
.link_libc = true,
});
exe.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c",
\\#include <stdio.h>
\\#include <foo/a.h>
\\#include <foo/sub_dir/b.h>
\\#include <foo/d.h>
\\#include <foo/config.h>
\\int main(void) {
\\ printf(FOO_A FOO_B FOO_D FOO_CONFIG_1 FOO_CONFIG_2);
\\ return 0;
\\}
) });
libfoo.installHeaders(.{ .path = "include" }, "foo", .{ .exclude_extensions = &.{".ignore_me.h"} });
libfoo.installHeader(b.addWriteFiles().add("d.h",
\\#define FOO_D "D"
\\
), "foo/d.h");
if (libfoo.installed_headers_include_tree != null) std.debug.panic("include tree step was created before linking", .{});
// Link before we have registered all headers for installation,
// to verify that the lazily created write files step is properly taken into account.
exe.linkLibrary(libfoo);
if (libfoo.installed_headers_include_tree == null) std.debug.panic("include tree step was not created after linking", .{});
libfoo.installConfigHeader(b.addConfigHeader(.{
.style = .blank,
.include_path = "foo/config.h",
}, .{
.FOO_CONFIG_1 = "1",
.FOO_CONFIG_2 = "2",
}));
const run_exe = b.addRunArtifact(exe);
run_exe.expectStdOutEqual("ABD12");
test_step.dependOn(&run_exe.step);
const install_exe = b.addInstallArtifact(libfoo, .{
.dest_dir = .{ .override = .{ .custom = "custom" } },
.h_dir = .{ .override = .{ .custom = "custom/include" } },
.implib_dir = .disabled,
.pdb_dir = .disabled,
});
const check_exists = b.addExecutable(.{
.name = "check_exists",
.root_source_file = .{ .path = "check_exists.zig" },
.target = b.resolveTargetQuery(.{}),
.optimize = .Debug,
});
const run_check_exists = b.addRunArtifact(check_exists);
run_check_exists.addArgs(&.{
"custom/include/foo/a.h",
"!custom/include/foo/ignore_me.txt",
"custom/include/foo/sub_dir/b.h",
"!custom/include/foo/sub_dir/c.ignore_me.h",
"custom/include/foo/d.h",
"custom/include/foo/config.h",
});
run_check_exists.setCwd(.{ .cwd_relative = b.getInstallPath(.prefix, "") });
run_check_exists.expectExitCode(0);
run_check_exists.step.dependOn(&install_exe.step);
test_step.dependOn(&run_check_exists.step);
}

View File

@ -0,0 +1,37 @@
const std = @import("std");
/// Checks the existence of files relative to cwd.
/// A path starting with ! should not exist.
pub fn main() !void {
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
var arg_it = try std.process.argsWithAllocator(arena);
_ = arg_it.next();
const cwd = std.fs.cwd();
const cwd_realpath = try cwd.realpathAlloc(arena, "");
while (arg_it.next()) |file_path| {
if (file_path.len > 0 and file_path[0] == '!') {
errdefer std.log.err(
"excluded file check '{s}{c}{s}' failed",
.{ cwd_realpath, std.fs.path.sep, file_path[1..] },
);
if (std.fs.cwd().statFile(file_path[1..])) |_| {
return error.FileFound;
} else |err| switch (err) {
error.FileNotFound => {},
else => return err,
}
} else {
errdefer std.log.err(
"included file check '{s}{c}{s}' failed",
.{ cwd_realpath, std.fs.path.sep, file_path },
);
_ = try std.fs.cwd().statFile(file_path);
}
}
}

View File

@ -0,0 +1 @@
#define FOO_A "A"

View File

@ -0,0 +1 @@
ignore me

View File

@ -0,0 +1 @@
#define FOO_B "B"

View File

@ -0,0 +1 @@
#error "ignore me"