diff options
| author | Ryan Liptak <squeek502@hotmail.com> | 2023-10-09 04:06:28 -0700 |
|---|---|---|
| committer | Ryan Liptak <squeek502@hotmail.com> | 2023-10-15 13:33:16 -0700 |
| commit | 5f15acc463d39baedd8de367330286b91c8bafc8 (patch) | |
| tree | 9b9afbdb5b05090665c1b9b596f874c4fa8dd74c /lib/std/Build.zig | |
| parent | b0f031f5730be496872591dce3242004cf9a6f22 (diff) | |
| download | zig-5f15acc463d39baedd8de367330286b91c8bafc8.tar.gz zig-5f15acc463d39baedd8de367330286b91c8bafc8.zip | |
Add preliminary support for Windows .manifest files
An embedded manifest file is really just XML data embedded as a RT_MANIFEST resource (ID = 24). Typically, the Windows-only 'Manifest Tool' (`mt.exe`) is used to embed manifest files, and `mt.exe` also seems to perform some transformation of the manifest data before embedding, but in testing it doesn't seem like the transformations are necessary to get the intended result.
So, to handle embedding manifest files, Zig now takes the following approach:
- Generate a .rc file with the contents `1 24 "path-to-manifest.manifest"`
- Compile that generated .rc file into a .res file
- Link the .res file into the final binary
This effectively achieves the same thing as `mt.exe` minus the validation/transformations of the XML data that it performs.
How this is used:
On the command line:
```
zig build-exe main.zig main.manifest
```
(on the command line, specifying a .manifest file when the target object format is not COFF is an error)
or in build.zig:
```
const exe = b.addExecutable(.{
.name = "manifest-test",
.root_source_file = .{ .path = "main.zig" },
.target = target,
.optimize = optimize,
.win32_manifest = .{ .path = "main.manifest" },
});
```
(in build.zig, the manifest file is ignored if the target object format is not COFF)
Note: Currently, only one manifest file can be specified per compilation. This is because the ID of the manifest resource is currently always 1. Specifying multiple manifests could be supported if a way for the user to specify an ID for each manifest is added (manifest IDs must be a u16).
Closes #17406
options
Diffstat (limited to 'lib/std/Build.zig')
| -rw-r--r-- | lib/std/Build.zig | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 5c80f4972c..f1c6761303 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -635,6 +635,12 @@ pub const ExecutableOptions = struct { use_lld: ?bool = null, zig_lib_dir: ?LazyPath = null, main_mod_path: ?LazyPath = null, + /// Embed a `.manifest` file in the compilation if the object format supports it. + /// https://learn.microsoft.com/en-us/windows/win32/sbscs/manifest-files-reference + /// Manifest files must have the extension `.manifest`. + /// Can be set regardless of target. The `.manifest` file will be ignored + /// if the target object format does not support embedded manifests. + win32_manifest: ?LazyPath = null, /// Deprecated; use `main_mod_path`. main_pkg_path: ?LazyPath = null, @@ -656,6 +662,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { .use_lld = options.use_lld, .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, .main_mod_path = options.main_mod_path orelse options.main_pkg_path, + .win32_manifest = options.win32_manifest, }); } @@ -706,6 +713,12 @@ pub const SharedLibraryOptions = struct { use_lld: ?bool = null, zig_lib_dir: ?LazyPath = null, main_mod_path: ?LazyPath = null, + /// Embed a `.manifest` file in the compilation if the object format supports it. + /// https://learn.microsoft.com/en-us/windows/win32/sbscs/manifest-files-reference + /// Manifest files must have the extension `.manifest`. + /// Can be set regardless of target. The `.manifest` file will be ignored + /// if the target object format does not support embedded manifests. + win32_manifest: ?LazyPath = null, /// Deprecated; use `main_mod_path`. main_pkg_path: ?LazyPath = null, @@ -727,6 +740,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile .use_lld = options.use_lld, .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, .main_mod_path = options.main_mod_path orelse options.main_pkg_path, + .win32_manifest = options.win32_manifest, }); } |
