Skip to content

Commit 471f279

Browse files
committed
Fix rc preprocessing when using the MinGW includes and targeting the GNU abi
Also update the standalone test so that this failure would have been detected on any host system.
1 parent 0168ed7 commit 471f279

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Compilation.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,6 +4513,19 @@ fn updateWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, win32
45134513
"-fms-compatibility", // Allow things like "header.h" to be resolved relative to the 'root' .rc file, among other things
45144514
"-DRC_INVOKED", // https://learn.microsoft.com/en-us/windows/win32/menurc/predefined-macros
45154515
});
4516+
// Using -fms-compatibility and targeting the gnu abi interact in a strange way:
4517+
// - Targeting the GNU abi stops _MSC_VER from being defined
4518+
// - Passing -fms-compatibility stops __GNUC__ from being defined
4519+
// Neither being defined is a problem for things like things like MinGW's
4520+
// vadefs.h, which will fail during preprocessing if neither are defined.
4521+
// So, when targeting the GNU abi, we need to force __GNUC__ to be defined.
4522+
//
4523+
// TODO: This is a workaround that should be removed if possible.
4524+
if (comp.getTarget().isGnu()) {
4525+
// This is the same default gnuc version that Clang uses:
4526+
// https://github.com/llvm/llvm-project/blob/4b5366c9512aa273a5272af1d833961e1ed156e7/clang/lib/Driver/ToolChains/Clang.cpp#L6738
4527+
try argv.append("-fgnuc-version=4.2.1");
4528+
}
45164529
for (options.extra_include_paths.items) |extra_include_path| {
45174530
try argv.append("--include-directory");
45184531
try argv.append(extra_include_path);

test/standalone/windows_resources/build.zig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ pub fn build(b: *std.Build) void {
1111
.abi = .gnu,
1212
};
1313

14-
add(b, native_target, test_step);
15-
add(b, cross_target, test_step);
14+
add(b, native_target, .any, test_step);
15+
add(b, cross_target, .any, test_step);
16+
17+
add(b, native_target, .gnu, test_step);
18+
add(b, cross_target, .gnu, test_step);
1619
}
1720

18-
fn add(b: *std.Build, target: std.zig.CrossTarget, test_step: *std.Build.Step) void {
21+
fn add(b: *std.Build, target: std.zig.CrossTarget, rc_includes: enum { any, gnu }, test_step: *std.Build.Step) void {
1922
const exe = b.addExecutable(.{
2023
.name = "zig_resource_test",
2124
.root_source_file = .{ .path = "main.zig" },
@@ -26,6 +29,10 @@ fn add(b: *std.Build, target: std.zig.CrossTarget, test_step: *std.Build.Step) v
2629
.file = .{ .path = "res/zig.rc" },
2730
.flags = &.{"/c65001"}, // UTF-8 code page
2831
});
32+
exe.rc_includes = switch (rc_includes) {
33+
.any => .any,
34+
.gnu => .gnu,
35+
};
2936

3037
_ = exe.getEmittedBin();
3138

0 commit comments

Comments
 (0)