Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trick the meson build system into thinking zig rc is rc.exe #23301

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

squeek502
Copy link
Collaborator

@squeek502 squeek502 commented Mar 20, 2025

(would appreciate this being included in 0.14.1)


When determining the type of RC compiler, meson passes /? or --version and then reads from stdout looking for particular string(s) anywhere in the output.

So, by adding the string "Microsoft Resource Compiler" to the /? output, meson will recognize zig rc as rc.exe and give it the correct options, which works fine since zig rc is drop-in CLI compatible with rc.exe.

This allows using zig rc with meson for (cross-)compiling, by either:

  • Setting WINDRES="zig rc" or putting windres = ['zig', 'rc'] in the cross-file
    • This will work like rc.exe, so it will output .res files. This will only link successfully if you are using a linker that can do .res -> .obj conversion (so something like zig cc, MSVC, lld)
  • Setting WINDRES="zig rc /:output-format coff" or putting windres = ['zig', 'rc', '/:output-format', 'coff'] in the cross-file
    • This will make meson pass flags as if it were rc.exe, but it will cause the resulting .res file to actually be a COFF object file, meaning it will work with any linker that handles COFF object files

Example cross file that uses zig cc (which can link .res files, so /:output-format coff is not necessary) and zig rc:

[binaries]
c = ['zig', 'cc', '--target=x86_64-windows-gnu']
windres = ['zig', 'rc']

[target_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

Using the test project here with the example cross file above to cross-compile from Linux:

$ meson setup builddir --cross-file zig-rc.txt
...
Windows resource compiler: Drop-in compatible with the Microsoft Resource Compiler.
...

$ cd builddir
$ meson compile
$ file check
check: PE32+ executable (console) x86-64, for MS Windows
$ wine ./check
hello world

(the hello world string comes from a resource)


Relevant links:

When determining the type of RC compiler, meson passes `/?` or `--version` and then reads from `stdout` looking for particular string(s) anywhere in the output.

So, by adding the string "Microsoft Resource Compiler" to the `/?` output, meson will recognize `zig rc` as rc.exe and give it the correct options, which works fine since `zig rc` is drop-in CLI compatible with rc.exe.

This allows using `zig rc` with meson for (cross-)compiling, by either:

- Setting WINDRES="zig rc" or putting windres = ['zig', 'rc'] in the cross-file
  + This will work like rc.exe, so it will output .res files. This will only link successfully if you are using a linker that can do .res -> .obj conversion (so something like zig cc, MSVC, lld)
- Setting WINDRES="zig rc /:output-format coff" or putting windres = ['zig', 'rc', '/:output-format', 'coff'] in the cross-file
  + This will make meson pass flags as if it were rc.exe, but it will cause the resulting .res file to actually be a COFF object file, meaning it will work with any linker that handles COFF object files

Example cross file that uses `zig cc` (which can link `.res` files, so `/:output-format coff` is not necessary) and `zig rc`:

```
[binaries]
c = ['zig', 'cc', '--target=x86_64-windows-gnu']
windres = ['zig', 'rc']

[target_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'
```
@alexrp alexrp added this to the 0.14.1 milestone Mar 20, 2025
@andrewrk andrewrk merged commit 9c9d393 into ziglang:master Mar 21, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants