Skip to content

Commit

Permalink
Add _WIN32 as a predefined macro
Browse files Browse the repository at this point in the history
This is not mentioned in https://learn.microsoft.com/en-us/windows/win32/menurc/predefined-macros but it is empirically defined by default in the Win32 RC compiler.
  • Loading branch information
squeek502 committed Aug 11, 2024
1 parent f85c8f4 commit 877fe6e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/preprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub fn appendAroArgs(arena: Allocator, argv: *std.ArrayList([]const u8), options
"--emulate=msvc",
"-nostdinc",
"-DRC_INVOKED",
"-D_WIN32", // undocumented, but defined by default
});
for (options.extra_include_paths.items) |extra_include_path| {
try argv.append("-I");
Expand Down
41 changes: 41 additions & 0 deletions test/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ test "including windows.h" {
try testExpectNoError(tmp.dir, &.{ exe_path, "/:auto-includes", "gnu", "test.rc" });
}

test "predefined macros" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();

try tmp.dir.writeFile(.{
.sub_path = "test.rc",
.data =
\\1 RCDATA { RC_INVOKED, _WIN32 }
,
});

try testExpectNoError(tmp.dir, &.{ exe_path, "test.rc" });
// case sensitive, so this doesn't undef RC_INVOKED
try testExpectNoError(tmp.dir, &.{ exe_path, "/u", "rc_invoked", "test.rc" });
// undefing predefined macros works
try testExpectError(tmp.dir, &.{ exe_path, "/u", "RC_INVOKED", "test.rc" });
try testExpectError(tmp.dir, &.{ exe_path, "/u", "_WIN32", "test.rc" });
}

fn testExpectNoError(dir: std.fs.Dir, argv: []const []const u8) !void {
const allocator = std.testing.allocator;

Expand All @@ -38,3 +57,25 @@ fn testExpectNoError(dir: std.fs.Dir, argv: []const []const u8) !void {
return error.UnexpectedErrors;
}
}

fn testExpectError(dir: std.fs.Dir, argv: []const []const u8) !void {
const allocator = std.testing.allocator;

const dir_path = try dir.realpathAlloc(allocator, ".");
defer allocator.free(dir_path);

const result = try std.process.Child.run(.{
.allocator = allocator,
.argv = argv,
.max_output_bytes = std.math.maxInt(u32),
.cwd = dir_path,
.cwd_dir = dir,
});
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);

if (result.term != .Exited or result.term.Exited == 0) {
std.debug.print("command unexpectedly succeeded:\n---\nstdout:\n---\n{s}\n---\nstderr:\n---\n{s}\n", .{ result.stdout, result.stderr });
return error.UnexpectedSuccess;
}
}

0 comments on commit 877fe6e

Please sign in to comment.