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

add to the toolchain ability to create an import library #21171

Closed
andrewrk opened this issue Aug 22, 2024 · 1 comment
Closed

add to the toolchain ability to create an import library #21171

andrewrk opened this issue Aug 22, 2024 · 1 comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows
Milestone

Comments

@andrewrk
Copy link
Member

Prerequisite of #16270.

zig/src/mingw.zig

Lines 279 to 289 in 205e17a

if (!build_options.have_llvm) return error.ZigCompilerNotBuiltWithLLVMExtensions;
const llvm_bindings = @import("codegen/llvm/bindings.zig");
const llvm = @import("codegen/llvm.zig");
const arch_tag = llvm.targetArch(target.cpu.arch);
const def_final_path_z = try arena.dupeZ(u8, def_final_path);
const lib_final_path_z = try arena.dupeZ(u8, lib_final_path);
if (llvm_bindings.WriteImportLibrary(def_final_path_z.ptr, arch_tag, lib_final_path_z.ptr, true)) {
// TODO surface a proper error here
log.err("unable to turn {s}.def into {s}.lib", .{ lib_name, lib_name });
return error.WritingImportLibFailed;
}

zig/src/zig_llvm.cpp

Lines 447 to 524 in 205e17a

bool ZigLLVMWriteImportLibrary(const char *def_path, const ZigLLVM_ArchType arch,
const char *output_lib_path, bool kill_at)
{
COFF::MachineTypes machine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
switch (arch) {
case ZigLLVM_x86:
machine = COFF::IMAGE_FILE_MACHINE_I386;
break;
case ZigLLVM_x86_64:
machine = COFF::IMAGE_FILE_MACHINE_AMD64;
break;
case ZigLLVM_arm:
case ZigLLVM_armeb:
case ZigLLVM_thumb:
case ZigLLVM_thumbeb:
machine = COFF::IMAGE_FILE_MACHINE_ARMNT;
break;
case ZigLLVM_aarch64:
case ZigLLVM_aarch64_be:
machine = COFF::IMAGE_FILE_MACHINE_ARM64;
break;
default:
break;
}
if (machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
return true;
}
auto bufOrErr = MemoryBuffer::getFile(def_path);
if (!bufOrErr) {
return false;
}
MemoryBuffer& buf = *bufOrErr.get();
Expected<object::COFFModuleDefinition> def =
object::parseCOFFModuleDefinition(buf, machine, /* MingwDef */ true);
if (!def) {
return true;
}
// The exports-juggling code below is ripped from LLVM's DllToolDriver.cpp
// If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
// Name with ExtName and clear ExtName. When only creating an import
// library and not linking, the internal name is irrelevant. This avoids
// cases where writeImportLibrary tries to transplant decoration from
// symbol decoration onto ExtName.
for (object::COFFShortExport& E : def->Exports) {
if (!E.ExtName.empty()) {
E.Name = E.ExtName;
E.ExtName.clear();
}
}
if (machine == COFF::IMAGE_FILE_MACHINE_I386 && kill_at) {
for (object::COFFShortExport& E : def->Exports) {
if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
continue;
E.SymbolName = E.Name;
// Trim off the trailing decoration. Symbols will always have a
// starting prefix here (either _ for cdecl/stdcall, @ for fastcall
// or ? for C++ functions). Vectorcall functions won't have any
// fixed prefix, but the function base name will still be at least
// one char.
E.Name = E.Name.substr(0, E.Name.find('@', 1));
// By making sure E.SymbolName != E.Name for decorated symbols,
// writeImportLibrary writes these symbols with the type
// IMPORT_NAME_UNDECORATE.
}
}
return static_cast<bool>(
object::writeImportLibrary(def->OutputFile, output_lib_path,
def->Exports, machine, /* MinGW */ true));
}

We need the ability to do this without the dependency on LLVM.

Probably this should be exposed as a CLI subcommand as well.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows labels Aug 22, 2024
@andrewrk andrewrk added this to the 0.15.0 milestone Aug 22, 2024
@andrewrk
Copy link
Member Author

Oops, duplicate of #17807.

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
@alexrp alexrp modified the milestones: 0.15.0, 0.14.0 Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows
Projects
None yet
Development

No branches or pull requests

2 participants