Support generating import libraries from mingw .def files without LLVM#25414
Merged
andrewrk merged 5 commits intoziglang:masterfrom Oct 6, 2025
Merged
Support generating import libraries from mingw .def files without LLVM#25414andrewrk merged 5 commits intoziglang:masterfrom
andrewrk merged 5 commits intoziglang:masterfrom
Conversation
86e87e8 to
0844d3a
Compare
Member
Author
|
Will likely wait until #25430 is merged and then rework the relevant parts of this PR accordingly (at the very least, the |
Previously, `setAlignment` would set the value to 1 fewer than it should, so if you were intending to set alignment to 8 bytes, it would actually set it to 4 bytes, etc.
Convenience function similar in nature to Symbol.sizeOf
Just makes this a bit nicer to work with since those fields only have 1 intended value.
For the supported COFF machine types of X64 (x86_64), I386 (x86), ARMNT (thumb), and ARM64 (aarch64), this new Zig implementation results in byte-for-byte identical .lib files when compared to the previous LLVM-backed implementation.
0844d3a to
e393543
Compare
andrewrk
approved these changes
Oct 6, 2025
Comment on lines
-341
to
-347
| pub const WriteImportLibrary = ZigLLVMWriteImportLibrary; | ||
| extern fn ZigLLVMWriteImportLibrary( | ||
| def_path: [*:0]const u8, | ||
| coff_machine: c_uint, | ||
| output_lib_path: [*:0]const u8, | ||
| kill_at: bool, | ||
| ) bool; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Eliminates a dependency on LLVM with regards to the set of MinGW .def files shipped with Zig. This implementation is largely based on the LLVM implementation (specifically COFFModuleDefinition.cpp and COFFImportFile.cpp).
How this was tested
I developed/tested this in a separate repository here (the
def.zig/implib.zigfiles are 1:1 between that repository and this PR)I have a branch of zig here that adds a
zig implibcommand that takes in a.deffile and outputs two files: a.pp.deffile that's the result of preprocessing with Aro (this is something that always happens duringbuildImportLib), and a.libfile that's the result of callingllvm_bindings.WriteImportLibraryafter preprocessing.I then have a
gen.zigscript here that walks Zig'slib/libc/mingwdirectory and spawnszig implibto create.pp.def/.libpairs for every.defand.def.infile found.Finally, I have a
check.zigscript here that walks a directory of these generated pairs and confirms that the new implementation generates a byte-for-byte identical.libfile for each.pp.definput.The
check.zigscript passes cleanly for the entirelib/libc/mingwdirectory when targeting all supportedwindows-gnutargets: x86_64, x86, thumb, and aarch64 (see here for why these are specified).(note that the byte-for-byte nature means that the implementation has inherited a few quirks from LLVM around size counts/padding; those quirks have been kept for now just to ensure there's no possible breakage, but that should be revisited when eliminating the LLD dependency for COFF)
How this fits into the bigger picture
There are effectively two parts to this implementation:
.deffile parsing.libfile writing (import library using the COFF archive file format)In theory, the second part could be made redundant once the self-hosted COFF linker is ready, since actually writing out the archive file format is not a necessary component of linking--all the required information exists in the
.deffile.However, the archive file writing implementation is not (in principle) COFF-specific (although the current implementation is), so in the future:
.deffile parsing as an input and skip the COFF import library generationIf #17807 is specifically about MinGW .def files, then this closes #17807