]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Driver: Add support for Windows resource files (.rc, .res) [PR108866]
authorPeter Damianov <peter0x44@disroot.org>
Thu, 11 Dec 2025 01:52:42 +0000 (01:52 +0000)
committerJonathan Yong <10walls@gmail.com>
Mon, 15 Dec 2025 14:56:01 +0000 (14:56 +0000)
This patch adds support for compiling Windows resource files
(.rc) and pre-compiled resource files (.res) directly through the
GCC driver on PECOFF targets.

Previously, users had to manually invoke windres to compile resource
files before linking:

    windres -o resource.o resource.rc
    gcc main.c resource.o -o program.exe

With this patch, GCC can handle resource files automatically:

    gcc main.c resource.rc -o program.exe
    gcc main.c resource.res -o program.exe

Now, for an explanation of each line of the spec:

If any of -E -M or -MM were passed, do nothing. No object files are output.
    "%{!E:%{!M:%{!MM:windres

Add -J so that windres does not perform autodetection of input type
Add -O so that the output type is always COFF
     -J rc -O coff \

For multilib configurations, tell windres to write out the correct COFF format.
- If -m32 is specified, use pe-i386 format
- If -m64 is specified, use pe-x86_64 format
- If neither are specified, use the correct default for the target

This is defined in WINDRES_FORMAT_SPEC which expands to:
  For 64-bit: "%{m32:-F pe-i386;m64|!m32:-F pe-x86-64}"
  For 32-bit: "%{m64:-F pe-x86-64;m32|!m64:-F pe-i386}"

Pass through -I -D -U on to windres, because it supports them.
    %{I*:-I%*} %{D*:-D%*} %{U*:-U%*} \

If -c is passed, pass through -o to windres, if it was specified. Otherwise,
 output to the input basename with .o suffix. Else, output to a
 temp file that will be deleted after linking.
    %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} %i}}}",

gcc/ChangeLog:

PR driver/108866
* gcc.cc (default_compilers): Add EXTRA_DEFAULT_COMPILERS so the config
of a target can add an extra compiler spec to default_compilers.
* config/i386/cygming.h (WINDRES_FORMAT_SPEC): New macro to handle
PE format selection based on TARGET_64BIT_DEFAULT and -m32/-m64 flags.
(EXTRA_DEFAULT_COMPILERS): Add spec for windres.
* config/aarch64/cygming.h (EXTRA_DEFAULT_COMPILERS): Likewise.

Signed-off-by: Peter Damianov <peter0x44@disroot.org>
Signed-off-by: Jonathan Yong <10walls@gmail.com>
gcc/config/aarch64/cygming.h
gcc/config/i386/cygming.h
gcc/gcc.cc

index 1c7f8f58e6452c52271f4d7f702eddf60d7111f6..19553cb01c510f22812ad1cf366f84db5e99e56a 100644 (file)
@@ -254,4 +254,18 @@ still needed for compilation.  */
 #undef  TARGET_ASM_LTO_END
 #define TARGET_ASM_LTO_END mingw_pe_asm_lto_end
 
+/* Support for Windows resource files.  */
+#define EXTRA_DEFAULT_COMPILERS \
+  {".rc", "@windres-rc", 0, 0, 0}, \
+  {"@windres-rc", \
+   "%{!E:%{!M:%{!MM:windres -J rc -O coff -F pe-aarch64 \
+      %{I*:-I%*} %{D*:-D%*} %{U*:-U%*} \
+      %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} %i}}}", \
+   0, 0, 0}, \
+  {".res", "@windres-res", 0, 0, 0}, \
+  {"@windres-res", \
+   "%{!E:%{!M:%{!MM:windres -J res -O coff -F pe-aarch64 \
+      %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} %i}}}", \
+   0, 0, 0},
+
 #endif
index 0a3173c4e937c62dd4d3a10b2f07b4b61faecc6b..8311697a2da0ea5a9d9797d6047151687e9ddd8b 100644 (file)
@@ -474,3 +474,27 @@ do {                                               \
   (ix86_cmodel == CM_LARGE_PIC || ix86_cmodel == CM_MEDIUM_PIC)
 
 #define HAVE_64BIT_POINTERS TARGET_64BIT_DEFAULT
+
+/* Support for Windows resource files.  */
+#if TARGET_64BIT_DEFAULT
+#define WINDRES_FORMAT_SPEC \
+      "%{m32:-F pe-i386;m64|!m32:-F pe-x86-64} "
+#else
+#define WINDRES_FORMAT_SPEC \
+      "%{m64:-F pe-x86-64;m32|!m64:-F pe-i386} "
+#endif
+
+#define EXTRA_DEFAULT_COMPILERS \
+  {".rc", "@windres-rc", 0, 0, 0}, \
+  {"@windres-rc", \
+   "%{!E:%{!M:%{!MM:windres -J rc -O coff " \
+      WINDRES_FORMAT_SPEC \
+      "%{I*:-I%*} %{D*:-D%*} %{U*:-U%*} \
+      %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} %i}}}", \
+   0, 0, 0}, \
+  {".res", "@windres-res", 0, 0, 0}, \
+  {"@windres-res", \
+   "%{!E:%{!M:%{!MM:windres -J res -O coff " \
+      WINDRES_FORMAT_SPEC \
+      "%{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} %i}}}", \
+   0, 0, 0},
index 39e9fde93002031654cca366349de96490a5c864..e6e28b17a072a199579573783fb7df500eb0be0f 100644 (file)
@@ -1517,6 +1517,11 @@ static const struct compiler default_compilers[] =
 #endif
    , 0, 0, 0},
 
+#ifndef EXTRA_DEFAULT_COMPILERS
+#define EXTRA_DEFAULT_COMPILERS
+#endif
+  EXTRA_DEFAULT_COMPILERS
+
 #include "specs.h"
   /* Mark end of table.  */
   {0, 0, 0, 0, 0}