Skip to content

Commit ddde99b

Browse files
squeek502andrewrk
authored andcommitted
Build system: Allow specifying Win32 resource include paths using LazyPath
Adds an `include_paths` field to RcSourceFile that takes a slice of LazyPaths. The paths are resolved and subsequently appended to the -rcflags as `/I <resolved path>`. This fixes an accidental regression from #19174. Before that PR, all Win32 resource compilation would inherit the CC flags (via `addCCArgs`), which included things like include directories. After that PR, though, that is no longer the case. However, this commit intentionally does not restore the previous behavior (inheriting the C include paths). Instead, each .rc file will need to have its include paths specified directly and the include paths only apply to one particular resource script. This allows more fine-grained control and has less potentially surprising behavior (at the cost of some convenience). Closes #19605
1 parent f2110b0 commit ddde99b

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

lib/std/Build/Module.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,18 @@ pub const RcSourceFile = struct {
110110
/// /x (ignore the INCLUDE environment variable)
111111
/// /D_DEBUG or /DNDEBUG depending on the optimization mode
112112
flags: []const []const u8 = &.{},
113+
/// Include paths that may or may not exist yet and therefore need to be
114+
/// specified as a LazyPath. Each path will be appended to the flags
115+
/// as `/I <resolved path>`.
116+
include_paths: []const LazyPath = &.{},
113117

114118
pub fn dupe(self: RcSourceFile, b: *std.Build) RcSourceFile {
119+
const include_paths = b.allocator.alloc(LazyPath, self.include_paths.len) catch @panic("OOM");
120+
for (include_paths, self.include_paths) |*dest, lazy_path| dest.* = lazy_path.dupe(b);
115121
return .{
116122
.file = self.file.dupe(b),
117123
.flags = b.dupeStrings(self.flags),
124+
.include_paths = include_paths,
118125
};
119126
}
120127
};
@@ -503,6 +510,9 @@ pub fn addWin32ResourceFile(m: *Module, source: RcSourceFile) void {
503510
rc_source_file.* = source.dupe(b);
504511
m.link_objects.append(allocator, .{ .win32_resource_file = rc_source_file }) catch @panic("OOM");
505512
addLazyPathDependenciesOnly(m, source.file);
513+
for (source.include_paths) |include_path| {
514+
addLazyPathDependenciesOnly(m, include_path);
515+
}
506516
}
507517

508518
pub fn addAssemblyFile(m: *Module, source: LazyPath) void {

lib/std/Build/Step/Compile.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12771277
.win32_resource_file => |rc_source_file| l: {
12781278
if (!my_responsibility) break :l;
12791279

1280-
if (rc_source_file.flags.len == 0) {
1280+
if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) {
12811281
if (prev_has_rcflags) {
12821282
try zig_args.append("-rcflags");
12831283
try zig_args.append("--");
@@ -1288,6 +1288,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12881288
for (rc_source_file.flags) |arg| {
12891289
try zig_args.append(arg);
12901290
}
1291+
for (rc_source_file.include_paths) |include_path| {
1292+
try zig_args.append("/I");
1293+
try zig_args.append(include_path.getPath2(module.owner, step));
1294+
}
12911295
try zig_args.append("--");
12921296
prev_has_rcflags = true;
12931297
}

test/standalone/windows_resources/build.zig

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

13-
add(b, b.host, .any, test_step);
14-
add(b, target, .any, test_step);
13+
const generated_h_step = b.addWriteFile("generated.h", "#define GENERATED_DEFINE \"foo\"");
1514

16-
add(b, b.host, .gnu, test_step);
17-
add(b, target, .gnu, test_step);
15+
add(b, b.host, .any, test_step, generated_h_step);
16+
add(b, target, .any, test_step, generated_h_step);
17+
18+
add(b, b.host, .gnu, test_step, generated_h_step);
19+
add(b, target, .gnu, test_step, generated_h_step);
1820
}
1921

2022
fn add(
2123
b: *std.Build,
2224
target: std.Build.ResolvedTarget,
2325
rc_includes: enum { any, gnu },
2426
test_step: *std.Build.Step,
27+
generated_h_step: *std.Build.Step.WriteFile,
2528
) void {
2629
const exe = b.addExecutable(.{
2730
.name = "zig_resource_test",
@@ -32,6 +35,9 @@ fn add(
3235
exe.addWin32ResourceFile(.{
3336
.file = b.path("res/zig.rc"),
3437
.flags = &.{"/c65001"}, // UTF-8 code page
38+
.include_paths = &.{
39+
.{ .generated = &generated_h_step.generated_directory },
40+
},
3541
});
3642
exe.rc_includes = switch (rc_includes) {
3743
.any => .any,

test/standalone/windows_resources/res/zig.rc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// This include file is generated via build.zig, and it #defines GENERATED_DEFINE
2+
#include "generated.h"
3+
FOO RCDATA { GENERATED_DEFINE }
4+
15
#define ICO_ID 1
26

37
// Nothing from windows.h is used in this .rc file,

0 commit comments

Comments
 (0)