authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-06-24 15:22:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-25 01:42:12-07:00
logb11170294052585ae81b9946d770f860d163da8f
treeeb4b73867e878ca8706b73f27eafa21ea6ccaa98
parent9684947faae674e093dda4c81d94f51b9568369e

Recognize the .res extension and link it as if it were an object file

.res files are compiled Windows resource files that get linked into executables/libraries. The linker knows what to do with them, but previously you had to trick Zig into thinking it was an object file (by renaming it to have the .obj extension, for example). After this commit, the following works: zig build-exe main.zig resource.res or, in build.zig: exe.addObjectFile("resource.res"); Closes #6488

2 files changed, 8 insertions(+), 3 deletions(-)

src/Compilation.zig+6-1
...@@ -4436,7 +4436,7 @@ pub fn addCCArgs(...@@ -4436,7 +4436,7 @@ pub fn addCCArgs(
4436 try argv.append("-fno-unwind-tables");4436 try argv.append("-fno-unwind-tables");
4437 }4437 }
4438 },4438 },
4439 .shared_library, .ll, .bc, .unknown, .static_library, .object, .def, .zig => {},4439 .shared_library, .ll, .bc, .unknown, .static_library, .object, .def, .zig, .res => {},
4440 .assembly, .assembly_with_cpp => {4440 .assembly, .assembly_with_cpp => {
4441 // The Clang assembler does not accept the list of CPU features like the4441 // The Clang assembler does not accept the list of CPU features like the
4442 // compiler frontend does. Therefore we must hard-code the -m flags for4442 // compiler frontend does. Therefore we must hard-code the -m flags for
...@@ -4602,6 +4602,7 @@ pub const FileExt = enum {...@@ -4602,6 +4602,7 @@ pub const FileExt = enum {
4602 static_library,4602 static_library,
4603 zig,4603 zig,
4604 def,4604 def,
4605 res,
4605 unknown,4606 unknown,
46064607
4607 pub fn clangSupportsDepFile(ext: FileExt) bool {4608 pub fn clangSupportsDepFile(ext: FileExt) bool {
...@@ -4617,6 +4618,7 @@ pub const FileExt = enum {...@@ -4617,6 +4618,7 @@ pub const FileExt = enum {
4617 .static_library,4618 .static_library,
4618 .zig,4619 .zig,
4619 .def,4620 .def,
4621 .res,
4620 .unknown,4622 .unknown,
4621 => false,4623 => false,
4622 };4624 };
...@@ -4639,6 +4641,7 @@ pub const FileExt = enum {...@@ -4639,6 +4641,7 @@ pub const FileExt = enum {
4639 .static_library => target.staticLibSuffix(),4641 .static_library => target.staticLibSuffix(),
4640 .zig => ".zig",4642 .zig => ".zig",
4641 .def => ".def",4643 .def => ".def",
4644 .res => ".res",
4642 .unknown => "",4645 .unknown => "",
4643 };4646 };
4644 }4647 }
...@@ -4730,6 +4733,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {...@@ -4730,6 +4733,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {
4730 return .cu;4733 return .cu;
4731 } else if (mem.endsWith(u8, filename, ".def")) {4734 } else if (mem.endsWith(u8, filename, ".def")) {
4732 return .def;4735 return .def;
4736 } else if (mem.endsWith(u8, filename, ".res")) {
4737 return .res;
4733 } else {4738 } else {
4734 return .unknown;4739 return .unknown;
4735 }4740 }
src/main.zig+2-2
...@@ -1510,7 +1510,7 @@ fn buildOutputType(...@@ -1510,7 +1510,7 @@ fn buildOutputType(
1510 }1510 }
1511 } else switch (file_ext orelse1511 } else switch (file_ext orelse
1512 Compilation.classifyFileExt(arg)) {1512 Compilation.classifyFileExt(arg)) {
1513 .object, .static_library, .shared_library => try link_objects.append(.{ .path = arg }),1513 .object, .static_library, .shared_library, .res => try link_objects.append(.{ .path = arg }),
1514 .assembly, .assembly_with_cpp, .c, .cpp, .h, .ll, .bc, .m, .mm, .cu => {1514 .assembly, .assembly_with_cpp, .c, .cpp, .h, .ll, .bc, .m, .mm, .cu => {
1515 try c_source_files.append(.{1515 try c_source_files.append(.{
1516 .src_path = arg,1516 .src_path = arg,
...@@ -1605,7 +1605,7 @@ fn buildOutputType(...@@ -1605,7 +1605,7 @@ fn buildOutputType(
1605 .ext = file_ext, // duped while parsing the args.1605 .ext = file_ext, // duped while parsing the args.
1606 });1606 });
1607 },1607 },
1608 .unknown, .shared_library, .object, .static_library => try link_objects.append(.{1608 .unknown, .shared_library, .object, .static_library, .res => try link_objects.append(.{
1609 .path = it.only_arg,1609 .path = it.only_arg,
1610 .must_link = must_link,1610 .must_link = must_link,
1611 }),1611 }),