authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-05-02 18:57:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-02 22:08:00-07:00
logea9d817a905ae19dcf27db4f380270485f9e26d2
treee8edc32b14154ca7f0e833953a8d18a787bf194d
parent8ea4283d83a23b2956a1894eeb64793335de82b9

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 https://github.com/ziglang/zig/pull/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

4 files changed, 29 insertions(+), 5 deletions(-)

lib/std/Build/Module.zig+10
......@@ -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
508518pub fn addAssemblyFile(m: *Module, source: LazyPath) void {
lib/std/Build/Step/Compile.zig+5-1
......@@ -1281,7 +1281,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12811281 .win32_resource_file => |rc_source_file| l: {
12821282 if (!my_responsibility) break :l;
12831283
1284 if (rc_source_file.flags.len == 0) {
1284 if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) {
12851285 if (prev_has_rcflags) {
12861286 try zig_args.append("-rcflags");
12871287 try zig_args.append("--");
......@@ -1292,6 +1292,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12921292 for (rc_source_file.flags) |arg| {
12931293 try zig_args.append(arg);
12941294 }
1295 for (rc_source_file.include_paths) |include_path| {
1296 try zig_args.append("/I");
1297 try zig_args.append(include_path.getPath2(module.owner, step));
1298 }
12951299 try zig_args.append("--");
12961300 prev_has_rcflags = true;
12971301 }
test/standalone/windows_resources/build.zig+10-4
......@@ -10,11 +10,13 @@ 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
2022fn add(
......@@ -22,6 +24,7 @@ fn add(
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+4
......@@ -1,3 +1,7 @@
1// This include file is generated via build.zig, and it #defines GENERATED_DEFINE
2#include "generated.h"
3FOO RCDATA { GENERATED_DEFINE }
4
15#define ICO_ID 1
26
37// Nothing from windows.h is used in this .rc file,