From 2ce32e44978428c367551c5c150d1ab12c0b73d7 Mon Sep 17 00:00:00 2001 From: MrDmitry Date: Wed, 24 Jan 2024 23:25:42 -0500 Subject: [PATCH] Extend standalone tests for cmakedefine --- test/standalone/cmakedefine/build.zig | 80 ++++++++++++++++--- .../{config.h.cmake => config.h.in} | 0 .../{expected.h => expected_config.h} | 0 test/standalone/cmakedefine/expected_error.h | 5 ++ test/standalone/cmakedefine/expected_pwd.sh | 1 + test/standalone/cmakedefine/expected_sigil.h | 5 ++ test/standalone/cmakedefine/expected_stack.h | 7 ++ .../standalone/cmakedefine/expected_wrapper.h | 30 +++++++ test/standalone/cmakedefine/pwd.sh.in | 1 + test/standalone/cmakedefine/sigil.h.in | 5 ++ test/standalone/cmakedefine/stack.h.in | 7 ++ test/standalone/cmakedefine/wrapper.h.in | 30 +++++++ 12 files changed, 158 insertions(+), 13 deletions(-) rename test/standalone/cmakedefine/{config.h.cmake => config.h.in} (100%) rename test/standalone/cmakedefine/{expected.h => expected_config.h} (100%) create mode 100644 test/standalone/cmakedefine/expected_error.h create mode 100644 test/standalone/cmakedefine/expected_pwd.sh create mode 100644 test/standalone/cmakedefine/expected_sigil.h create mode 100644 test/standalone/cmakedefine/expected_stack.h create mode 100644 test/standalone/cmakedefine/expected_wrapper.h create mode 100644 test/standalone/cmakedefine/pwd.sh.in create mode 100644 test/standalone/cmakedefine/sigil.h.in create mode 100644 test/standalone/cmakedefine/stack.h.in create mode 100644 test/standalone/cmakedefine/wrapper.h.in diff --git a/test/standalone/cmakedefine/build.zig b/test/standalone/cmakedefine/build.zig index 35dd431946..52fda369a6 100644 --- a/test/standalone/cmakedefine/build.zig +++ b/test/standalone/cmakedefine/build.zig @@ -4,7 +4,8 @@ const ConfigHeader = std.Build.Step.ConfigHeader; pub fn build(b: *std.Build) void { const config_header = b.addConfigHeader( .{ - .style = .{ .cmake = .{ .path = "config.h.cmake" } }, + .style = .{ .cmake = .{ .path = "config.h.in" } }, + .include_path = "config.h", }, .{ .noval = null, @@ -25,32 +26,85 @@ pub fn build(b: *std.Build) void { }, ); + const pwd_sh = b.addConfigHeader( + .{ + .style = .{ .cmake = .{ .path = "pwd.sh.in" } }, + .include_path = "pwd.sh", + }, + .{ .DIR = "${PWD}" }, + ); + + const sigil_header = b.addConfigHeader( + .{ + .style = .{ .cmake = .{ .path = "sigil.h.in" } }, + .include_path = "sigil.h", + }, + .{}, + ); + + const stack_header = b.addConfigHeader( + .{ + .style = .{ .cmake = .{ .path = "stack.h.in" } }, + .include_path = "stack.h", + }, + .{ + .AT = "@", + .UNDERSCORE = "_", + .NEST_UNDERSCORE_PROXY = "UNDERSCORE", + .NEST_PROXY = "NEST_UNDERSCORE_PROXY", + }, + ); + + const wrapper_header = b.addConfigHeader( + .{ + .style = .{ .cmake = .{ .path = "wrapper.h.in" } }, + .include_path = "wrapper.h", + }, + .{ + .DOLLAR = "$", + .TEXT = "TRAP", + + .STRING = "TEXT", + .STRING_AT = "@STRING@", + .STRING_CURLY = "{STRING}", + .STRING_VAR = "${STRING}", + }, + ); + const test_step = b.step("test", "Test it"); test_step.makeFn = compare_headers; test_step.dependOn(&config_header.step); + test_step.dependOn(&pwd_sh.step); + test_step.dependOn(&sigil_header.step); + test_step.dependOn(&stack_header.step); + test_step.dependOn(&wrapper_header.step); } fn compare_headers(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { _ = prog_node; const allocator = step.owner.allocator; - const cmake_header_path = "expected.h"; + const expected_fmt = "expected_{s}"; - const config_header_step = step.dependencies.getLast(); - const config_header = @fieldParentPtr(ConfigHeader, "step", config_header_step); + for (step.dependencies.items) |config_header_step| { + const config_header = @fieldParentPtr(ConfigHeader, "step", config_header_step); - const zig_header_path = config_header.output_file.path orelse @panic("Could not locate header file"); + const zig_header_path = config_header.output_file.path orelse @panic("Could not locate header file"); - const cwd = std.fs.cwd(); + const cwd = std.fs.cwd(); - const cmake_header = try cwd.readFileAlloc(allocator, cmake_header_path, config_header.max_bytes); - defer allocator.free(cmake_header); + const cmake_header_path = try std.fmt.allocPrint(allocator, expected_fmt, .{std.fs.path.basename(zig_header_path)}); + defer allocator.free(cmake_header_path); - const zig_header = try cwd.readFileAlloc(allocator, zig_header_path, config_header.max_bytes); - defer allocator.free(zig_header); + const cmake_header = try cwd.readFileAlloc(allocator, cmake_header_path, config_header.max_bytes); + defer allocator.free(cmake_header); - const header_text_index = std.mem.indexOf(u8, zig_header, "\n") orelse @panic("Could not find comment in header filer"); + const zig_header = try cwd.readFileAlloc(allocator, zig_header_path, config_header.max_bytes); + defer allocator.free(zig_header); - if (!std.mem.eql(u8, zig_header[header_text_index + 1 ..], cmake_header)) { - @panic("processed cmakedefine header does not match expected output"); + const header_text_index = std.mem.indexOf(u8, zig_header, "\n") orelse @panic("Could not find comment in header filer"); + + if (!std.mem.eql(u8, zig_header[header_text_index + 1 ..], cmake_header)) { + @panic("processed cmakedefine header does not match expected output"); + } } } diff --git a/test/standalone/cmakedefine/config.h.cmake b/test/standalone/cmakedefine/config.h.in similarity index 100% rename from test/standalone/cmakedefine/config.h.cmake rename to test/standalone/cmakedefine/config.h.in diff --git a/test/standalone/cmakedefine/expected.h b/test/standalone/cmakedefine/expected_config.h similarity index 100% rename from test/standalone/cmakedefine/expected.h rename to test/standalone/cmakedefine/expected_config.h diff --git a/test/standalone/cmakedefine/expected_error.h b/test/standalone/cmakedefine/expected_error.h new file mode 100644 index 0000000000..82e7731304 --- /dev/null +++ b/test/standalone/cmakedefine/expected_error.h @@ -0,0 +1,5 @@ +// bad interleave +#define @STRV${AL_AT@TEXT} + +// bad interleave +#define ${STRV@AL_AT}TEXT@ diff --git a/test/standalone/cmakedefine/expected_pwd.sh b/test/standalone/cmakedefine/expected_pwd.sh new file mode 100644 index 0000000000..7e1d44c8eb --- /dev/null +++ b/test/standalone/cmakedefine/expected_pwd.sh @@ -0,0 +1 @@ +echo ${PWD} diff --git a/test/standalone/cmakedefine/expected_sigil.h b/test/standalone/cmakedefine/expected_sigil.h new file mode 100644 index 0000000000..248daaa6cc --- /dev/null +++ b/test/standalone/cmakedefine/expected_sigil.h @@ -0,0 +1,5 @@ +#define VAR +#define AT @ +#define ATAT @@ +#define ATATAT @@@ +#define ATATATAT @@@@ diff --git a/test/standalone/cmakedefine/expected_stack.h b/test/standalone/cmakedefine/expected_stack.h new file mode 100644 index 0000000000..fb8876f2b9 --- /dev/null +++ b/test/standalone/cmakedefine/expected_stack.h @@ -0,0 +1,7 @@ +#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY +#define UNDERSCORE UNDERSCORE + +#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY +#define UNDERSCORE UNDERSCORE + +#define (empty) diff --git a/test/standalone/cmakedefine/expected_wrapper.h b/test/standalone/cmakedefine/expected_wrapper.h new file mode 100644 index 0000000000..ad052b20fd --- /dev/null +++ b/test/standalone/cmakedefine/expected_wrapper.h @@ -0,0 +1,30 @@ +// becomes TEXT +#define TEXT +#define TEXT + +// becomes `at`TEXT`at` +#define @TEXT@ +#define @TEXT@ + +// becomes TRAP +#define TRAP + +// becomes `dollar sign`{STRING} +#define ${STRING} +#define ${STRING} + +// becomes `dollar sign`{STRING} +#define ${STRING} +#define ${STRING} + +// becomes `dollar sign`{TEXT} +#define ${TEXT} +#define ${TEXT} + +// becomes `at`STRING`at` +#define @STRING@ +#define @STRING@ + +// becomes `empty` +#define +#define diff --git a/test/standalone/cmakedefine/pwd.sh.in b/test/standalone/cmakedefine/pwd.sh.in new file mode 100644 index 0000000000..9e4d91207e --- /dev/null +++ b/test/standalone/cmakedefine/pwd.sh.in @@ -0,0 +1 @@ +echo @DIR@ diff --git a/test/standalone/cmakedefine/sigil.h.in b/test/standalone/cmakedefine/sigil.h.in new file mode 100644 index 0000000000..0f64688ba7 --- /dev/null +++ b/test/standalone/cmakedefine/sigil.h.in @@ -0,0 +1,5 @@ +#define VAR ${} +#define AT @ +#define ATAT @@ +#define ATATAT @@@ +#define ATATATAT @@@@ diff --git a/test/standalone/cmakedefine/stack.h.in b/test/standalone/cmakedefine/stack.h.in new file mode 100644 index 0000000000..d4322f5990 --- /dev/null +++ b/test/standalone/cmakedefine/stack.h.in @@ -0,0 +1,7 @@ +#define NEST_UNDERSCORE_PROXY ${NEST${UNDERSCORE}PROXY} +#define UNDERSCORE @NEST@UNDERSCORE@PROXY@ + +#define NEST_UNDERSCORE_PROXY ${NEST${${NEST_UNDERSCORE${UNDERSCORE}PROXY}}PROXY} +#define UNDERSCORE @NEST@@NEST_UNDERSCORE@UNDERSCORE@PROXY@@PROXY@ + +#define (empty) ${NEST${${AT}UNDERSCORE${AT}}PROXY} diff --git a/test/standalone/cmakedefine/wrapper.h.in b/test/standalone/cmakedefine/wrapper.h.in new file mode 100644 index 0000000000..8985186c19 --- /dev/null +++ b/test/standalone/cmakedefine/wrapper.h.in @@ -0,0 +1,30 @@ +// becomes TEXT +#define @STRING@ +#define ${STRING} + +// becomes `at`TEXT`at` +#define @${STRING}@ +#define @@STRING@@ + +// becomes TRAP +#define ${@STRING@} + +// becomes `dollar sign`{STRING} +#define $@STRING_CURLY@ +#define $${STRING_CURLY} + +// becomes `dollar sign`{STRING} +#define @STRING_VAR@ +#define ${STRING_VAR} + +// becomes `dollar sign`{TEXT} +#define ${DOLLAR}{${STRING}} +#define @DOLLAR@{${STRING}} + +// becomes `at`STRING`at` +#define ${STRING_AT} +#define @STRING_AT@ + +// becomes `empty` +#define ${${STRING_VAR}} +#define ${@STRING_VAR@}